java.io.File temp = java.io.File.createTempFile(saveAsIOFile.getName(),UtilMethods.getFileExtension(saveAsIOFile.getName()));
temp.deleteOnExit();
FileUtil.copyFile(saveAsIOFile, temp);
FileAPI fAPI = APILocator.getFileAPI();
user = WebAPILocator.getUserWebAPI().getLoggedInUser(request);
Identifier ident = APILocator.getIdentifierAPI().findFromInode(inode);
if(ident!=null && InodeUtils.isSet(ident.getId()) && ident.getAssetType().equals("contentlet")){
Contentlet cont = APILocator.getContentletAPI().findContentletByIdentifier(ident.getId(), true, APILocator.getLanguageAPI().getDefaultLanguage().getId(), user, false);
if(cont!=null && InodeUtils.isSet(cont.getInode())){
Host h = APILocator.getHostAPI().find(cont.getHost(), user, false);
Folder folder = APILocator.getFolderAPI().findFolderByPath(ident.getParentPath(), h, user, false);
if(APILocator.getFileAssetAPI().fileNameExists(h, folder, fileName, cont.getIdentifier())){
response.getWriter().println("failure. fileAlreadyExists");
return;
}
Contentlet fileAsset = new Contentlet();
fileAsset.setStructureInode(folder.getDefaultFileType());
fileAsset.setStringProperty(FileAssetAPI.TITLE_FIELD, UtilMethods.getFileName(fileName));
fileAsset.setFolder(folder.getInode());
fileAsset.setHost(h.getIdentifier());
fileAsset.setBinary(FileAssetAPI.BINARY_FIELD, temp);
APILocator.getContentletAPI().checkin(fileAsset, user,false);
}
}else{
File src = fAPI.get(inode,user, false );
Identifier fileId = APILocator.getIdentifierAPI().find(src.getIdentifier());
Host h = APILocator.getHostAPI().find(fileId.getHostId(), user, false);
Folder folder = APILocator.getFileAPI().getFileFolder(src, h, user, false);
File copiedFile = new File();
copiedFile.setFileName(fileName);
copiedFile.setAuthor(user.getFullName());
copiedFile.setModUser(user.getUserId());
copiedFile.setFriendlyName(src.getFriendlyName());
copiedFile.setMimeType(APILocator.getFileAPI().getMimeType(fileName));
String x = WorkingCache.getPathFromCache(APILocator.getIdentifierAPI().find(folder).getPath() + fileName, h);
if(UtilMethods.isSet(x)){
response.getWriter().println("failure. fileAlreadyExists");
return;
}
fAPI.saveFile(copiedFile, temp, folder, user, false);
}
response.getWriter().println("success. File Saved");