* This method save the imported file on preview file system and return the bean that represent the file.
*
* May 7, 2012 - 4:17:26 PM
*/
public static PreviewFileAsset savePreviewFileAsset(Versionable asset) throws IOException, DotDataException, DotSecurityException{
PreviewFileAsset result = new PreviewFileAsset();
String today = SDF.format(new GregorianCalendar().getTime());
java.io.File todayPreviewAssetDir = new java.io.File(new java.io.File(FileUtil.getRealPath(Config.getStringProperty("PREVIEW_ASSET_PATH"))), today);
if(!todayPreviewAssetDir.exists())
todayPreviewAssetDir.mkdir();
java.io.File importedFilesAssetDir = new java.io.File(todayPreviewAssetDir, "files");
if(!importedFilesAssetDir.exists())
importedFilesAssetDir.mkdir();
java.io.File templatesAssetDir = new java.io.File(todayPreviewAssetDir, "templates");
if(!templatesAssetDir.exists())
templatesAssetDir.mkdir();
java.io.File assetFile = null;
java.io.File previewAsset = null;
// if the file imported is a FileAsset/Contentlet...
if(asset instanceof Contentlet){
Contentlet c = (Contentlet)asset;
assetFile = APILocator.getContentletAPI().getBinaryFile(c.getInode(), FileAssetAPI.BINARY_FIELD, APILocator.getUserAPI().getSystemUser());
previewAsset = new java.io.File(importedFilesAssetDir, assetFile.getName());
if(previewAsset.exists())
previewAsset.delete();
result.setContentlet(true);
result.setInode(c.getInode());
result.setParent(c.getFolder());
}else if(asset instanceof File){ // is a File
File f = (File)asset;
assetFile = APILocator.getFileAPI().getAssetIOFile(f);
previewAsset = new java.io.File(importedFilesAssetDir, assetFile.getName());
if(previewAsset.exists())
previewAsset.delete();
result.setContentlet(false);
result.setInode(f.getInode());
result.setParent(f.getParent());
}
//set the real path for the body preview
result.setRealFileSystemPath(previewAsset.getPath().substring(previewAsset.getPath().indexOf("/_preview")));
FileInputStream fis = new FileInputStream(assetFile);
FileOutputStream fos = new FileOutputStream(previewAsset);
final byte[] buffer = new byte[ 1024 ];
int n = 0;
while ((n = fis.read(buffer)) > 0){