use of org.irods.jargon.core.pub.io.IRODSFileInputStream in project metalnx-web by irods-contrib.
the class PreviewServiceImpl method filePreview.
@Override
public boolean filePreview(String path, String mimeType, HttpServletResponse response) {
logger.info("getting file preview for {} ::" + path + " and mimetype :: " + mimeType);
boolean isCopySuccessFul = true;
IRODSFileInputStream irodsFileInputStream = null;
IRODSFile irodsFile = null;
try {
IRODSFileFactory irodsFileFactory = irodsServices.getIRODSFileFactory();
irodsFile = irodsFileFactory.instanceIRODSFile(path);
irodsFileInputStream = irodsFileFactory.instanceIRODSFileInputStream(irodsFile);
response.setContentType(mimeType);
FileCopyUtils.copy(irodsFileInputStream, response.getOutputStream());
} catch (IOException | JargonException | DataGridConnectionRefusedException e) {
e.printStackTrace();
isCopySuccessFul = false;
} finally {
try {
if (irodsFileInputStream != null)
irodsFileInputStream.close();
if (irodsFile != null)
irodsFile.close();
} catch (Exception e) {
logger.error("Could not close stream(s): ", e.getMessage());
}
}
return isCopySuccessFul;
}
use of org.irods.jargon.core.pub.io.IRODSFileInputStream in project metalnx-web by irods-contrib.
the class FileOperationServiceImpl method copyFileIntoHttpResponse.
/**
* Copies a buffered input stream from a file to a HTTP response for
* downloading.
*
* @param path
* path to the file in iRODS to be added to the HTTP response
* @param response
* HTTP response to let the user download the file
* @return True, if the file was successfully added to the HTTP response. False,
* otherwise.
* @throws DataGridConnectionRefusedException
* is Metalnx cannot connect to the data grid
*/
private boolean copyFileIntoHttpResponse(String path, HttpServletResponse response) throws DataGridConnectionRefusedException {
boolean isCopySuccessFul = true;
IRODSFileInputStream irodsFileInputStream = null;
IRODSFile irodsFile = null;
logger.debug("Trying to copy path stream {} to user", path);
try {
String fileName = path.substring(path.lastIndexOf("/") + 1, path.length());
logger.debug("The filename is [{}]", fileName);
logger.debug("Initiating iRodsFileFactory");
IRODSFileFactory irodsFileFactory = irodsServices.getIRODSFileFactory();
logger.debug("Getting iRodsFileFactory instance for {}", path);
irodsFile = irodsFileFactory.instanceIRODSFile(path);
logger.debug("Creating stream from {}", irodsFile);
irodsFileInputStream = irodsFileFactory.instanceIRODSFileInputStream(irodsFile);
// set file mime type
response.setContentType(CONTENT_TYPE);
response.setHeader("Content-Disposition", String.format(HEADER_FORMAT, fileName));
response.setContentLength((int) irodsFile.length());
FileCopyUtils.copy(irodsFileInputStream, response.getOutputStream());
} catch (IOException e) {
logger.error("Could not put the file in the Http response ", e);
isCopySuccessFul = false;
} catch (JargonException e) {
logger.error("Could not copy file in the Http response: ", e.getMessage());
isCopySuccessFul = false;
} finally {
try {
if (irodsFileInputStream != null)
irodsFileInputStream.close();
if (irodsFile != null)
irodsFile.close();
} catch (Exception e) {
logger.error("Could not close stream(s): ", e.getMessage());
}
}
return isCopySuccessFul;
}
Aggregations