Skip to content

Commit

Permalink
Updated to new Path based version of BIMserver
Browse files Browse the repository at this point in the history
  • Loading branch information
rubendel committed Oct 15, 2015
1 parent 0dae54a commit 4a691c3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 55 deletions.
29 changes: 6 additions & 23 deletions Collada/src/org/bimserver/collada/Collada2GLTFThread.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
package org.bimserver.collada;

/******************************************************************************
* Copyright (C) 2009-2015 BIMserver.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -31,15 +14,15 @@ public class Collada2GLTFThread implements Runnable {
public boolean crashed = false;

// File-related.
private File basePath = null;
private Path basePath = null;
private Collada2GLTFConfiguration configuration = new Collada2GLTFConfiguration();

public Collada2GLTFThread(File file, File basePath){
this.configuration = new Collada2GLTFConfiguration(file.getName());
public Collada2GLTFThread(Path file, Path basePath){
this.configuration = new Collada2GLTFConfiguration(file.getFileName().toString());
this.basePath = basePath;
}

public Collada2GLTFThread(File basePath, Collada2GLTFConfiguration configuration){
public Collada2GLTFThread(Path basePath, Collada2GLTFConfiguration configuration){
this.basePath = basePath;
this.configuration = configuration;
}
Expand All @@ -51,7 +34,7 @@ public void run() {
// Build the process.
ProcessBuilder builder = new ProcessBuilder(configuration.getCall());
// Set the working directory to the place where the DAE is.
builder.directory(basePath);
builder.directory(basePath.toFile());
try {
// Attempt to run the subprocess.
Process p = builder.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -35,7 +33,6 @@
import java.util.zip.ZipOutputStream;

import org.apache.commons.codec.binary.Base64OutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.bimserver.collada.Collada2GLTFThread.Collada2GLTFConfiguration;
import org.bimserver.emf.IfcModelInterface;
Expand All @@ -46,6 +43,7 @@
import org.bimserver.plugins.serializers.ProgressReporter;
import org.bimserver.plugins.serializers.ProjectInfo;
import org.bimserver.plugins.serializers.SerializerException;
import org.bimserver.utils.PathUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -147,8 +145,9 @@ else if (returnType == ".json")
} finally {
// Attempt to clean up the temporary directory created by this serializer.
try {
if (writeDirectory != null && writeDirectory.exists())
FileUtils.deleteDirectory(writeDirectory);
if (writeDirectory != null && Files.exists(writeDirectory)) {
PathUtils.removeDirectoryWithContent(writeDirectory);
}
} catch (IOException ioe) {}
}
setMode(Mode.FINISHED);
Expand All @@ -159,22 +158,23 @@ else if (returnType == ".json")
return false;
}

private void jsonTheDirectory(OutputStream outputStream, File writeDirectory) throws IOException, UnsupportedEncodingException {
private void jsonTheDirectory(OutputStream outputStream, Path writeDirectory) throws IOException, UnsupportedEncodingException {
OutputStream jsonOutputStream = outputStream;
// Write the opening brace and a new-line.
jsonOutputStream.write(String.format("{%n").getBytes());
// Put the individual files into a JSON file.
for (File f : writeDirectory.listFiles(ignoreDAEFilter))
addFileToJSON(jsonOutputStream, f);
// Put the individual files into a JSON file.
for (Path f : PathUtils.getDirectories(writeDirectory)) {
addFileToJSON(jsonOutputStream, f);
}
// Write the closing brace.
jsonOutputStream.write(String.format("}").getBytes());
// Push the data into the parent stream (gets returned to the server).
jsonOutputStream.flush();
}

private void addFileToJSON(OutputStream jsonOutputStream, File f) throws IOException, UnsupportedEncodingException {
private void addFileToJSON(OutputStream jsonOutputStream, Path f) throws IOException, UnsupportedEncodingException {
// Use the file name as the key: file.ext
String key = f.getName();
String key = f.getFileName().toString();
// Create a place to store the base64 bytes.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Base64 encode the file.
Expand All @@ -183,38 +183,40 @@ private void addFileToJSON(OutputStream jsonOutputStream, File f) throws IOExcep
jsonOutputStream.write(String.format("\t\"%s\": \"%s,%s\",%n", key, "data:text/plain;base64", stream.toString("UTF-8")).getBytes());
}

public void encodeFileToBase64Stream(File file, OutputStream base64OutputStream) throws IOException {
InputStream is = new FileInputStream(file);
public void encodeFileToBase64Stream(Path file, OutputStream base64OutputStream) throws IOException {
InputStream inputStream = Files.newInputStream(file);
OutputStream out = new Base64OutputStream(base64OutputStream, true);
IOUtils.copy(is, out);
is.close();
IOUtils.copy(inputStream, out);
inputStream.close();
out.close();
}

private void zipTheDirectory(OutputStream outputStream, File writeDirectory) throws IOException {
private void zipTheDirectory(OutputStream outputStream, Path writeDirectory) throws IOException {
// Create the archive.
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
// Copy the files into the ZIP file.
for (File f : writeDirectory.listFiles(ignoreDAEFilter))
addToZipFile(f, zipOutputStream);
// Copy the files into the ZIP file.
for (Path f : PathUtils.getDirectories(writeDirectory)) {
addToZipFile(f, zipOutputStream);
}
// Push the data into the parent stream (gets returned to the server).
zipOutputStream.finish();
zipOutputStream.flush();
}

private void exportToGLTF(File writeDirectory) throws IOException, FileNotFoundException, SerializerException {
File colladaFile = new File(writeDirectory, projectInfo.getName() + ".dae");
private void exportToGLTF(Path writeDirectory) throws IOException, FileNotFoundException, SerializerException {
Path colladaFile = writeDirectory.resolve(projectInfo.getName() + ".dae");
// Create the Collada file: example.dae
if (!colladaFile.exists())
colladaFile.createNewFile();
// Prepare to write the Collada file.
FileOutputStream fileOutputStream = new FileOutputStream(colladaFile);
if (!Files.exists(colladaFile)) {
Files.createFile(colladaFile);
}
// Prepare to write the Collada file.
OutputStream outputStream = Files.newOutputStream(colladaFile);
// Write into the Collada file.
colladaSerializer.write(fileOutputStream, null);
colladaSerializer.write(outputStream, null);
// Push the data into the stream.
fileOutputStream.flush();
outputStream.flush();
// Finalize the stream and close the file.
fileOutputStream.close();
outputStream.close();
// Launch a thread to run the collada2gltf converter.
Collada2GLTFThread thread = new Collada2GLTFThread(colladaFile, writeDirectory);
synchronized (thread) {
Expand All @@ -227,15 +229,15 @@ private void exportToGLTF(File writeDirectory) throws IOException, FileNotFoundE
}
}

public void addToZipFile(File file, ZipOutputStream outputStream) throws FileNotFoundException, IOException {
public void addToZipFile(Path file, ZipOutputStream outputStream) throws FileNotFoundException, IOException {
// Get file name: example.file
String fileName = file.getName();
String fileName = file.getFileName().toString();
// Create an abstraction for how it will appear in the ZIP file.
ZipEntry zipEntry = new ZipEntry(fileName);
// Write the file's abstraction into the ZIP file.
outputStream.putNextEntry(zipEntry);
// Prepare to read the actual file.
FileInputStream inputStream = new FileInputStream(file);
// Prepare to read the actual file.
InputStream inputStream = Files.newInputStream(file);
// Buffer the file 4 kilobytes at a time.
byte[] bytes = new byte[4096];
// Read the file to its conclusion, writing out the information on the way.
Expand Down

0 comments on commit 4a691c3

Please sign in to comment.