use of org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream in project che by eclipse.
the class LocalVirtualFileSystem method getContent.
InputStream getContent(LocalVirtualFile virtualFile) throws ForbiddenException, ServerException {
if (virtualFile.isFile()) {
final PathLockFactory.PathLock lock = pathLockFactory.getLock(virtualFile.getPath(), false).acquire(WAIT_FOR_FILE_LOCK_TIMEOUT);
File spoolFile = null;
try {
final File ioFile = virtualFile.toIoFile();
final long fileLength = ioFile.length();
if (fileLength <= MAX_BUFFER_SIZE) {
return new ByteArrayInputStream(Files.toByteArray(ioFile));
}
// Copy this file to be able release the file lock before leave this method.
spoolFile = File.createTempFile("spool_file", null);
Files.copy(ioFile, spoolFile);
return new DeleteOnCloseFileInputStream(spoolFile);
} catch (IOException e) {
if (spoolFile != null) {
FileCleaner.addFile(spoolFile);
}
String errorMessage = String.format("Unable get content of '%s'", virtualFile.getPath());
LOG.error(errorMessage + "\n" + e.getMessage(), e);
throw new ServerException(errorMessage);
} finally {
lock.release();
}
} else {
throw new ForbiddenException(String.format("Unable get content. Item '%s' is not a file", virtualFile.getPath()));
}
}
use of org.eclipse.che.api.vfs.util.DeleteOnCloseFileInputStream in project che by eclipse.
the class SubversionApi method exportPath.
/**
* Perform an "svn export" based on the request.
*
* @param projectPath
* project path
* @param path
* exported path
* @param revision
* specified revision to export
* @return Response which contains hyperlink with download url
* @throws IOException
* if there is a problem executing the command
* @throws ServerException
* if there is an exporting issue
*/
public Response exportPath(@NotNull final String projectPath, @NotNull final String path, String revision) throws IOException, ServerException, UnauthorizedException {
final File project = new File(projectPath);
final List<String> uArgs = defaultArgs();
if (!isNullOrEmpty(revision)) {
addOption(uArgs, "--revision", revision);
}
uArgs.add("--force");
uArgs.add("export");
File tempDir = null;
File zip = null;
try {
tempDir = Files.createTempDir();
final CommandLineResult result = runCommand(null, uArgs, project, Arrays.asList(path, tempDir.getAbsolutePath()));
if (result.getExitCode() != 0) {
LOG.warn("Svn export process finished with exit status {}", result.getExitCode());
throw new ServerException("Export failed");
}
zip = new File(Files.createTempDir(), "export.zip");
ZipUtils.zipDir(tempDir.getPath(), tempDir, zip, IoUtil.ANY_FILTER);
} finally {
if (tempDir != null) {
IoUtil.deleteRecursive(tempDir);
}
}
final Response.ResponseBuilder responseBuilder = Response.ok(new DeleteOnCloseFileInputStream(zip), MediaType.ZIP.toString()).lastModified(new Date(zip.lastModified())).header(HttpHeaders.CONTENT_LENGTH, Long.toString(zip.length())).header("Content-Disposition", "attachment; filename=\"export.zip\"");
return responseBuilder.build();
}
Aggregations