use of org.craftercms.studio.model.rest.CmisUploadItem in project studio by craftercms.
the class CmisServiceImpl method uploadContent.
@Override
@HasPermission(type = DefaultPermission.class, action = "upload_content_cmis")
public CmisUploadItem uploadContent(@ProtectedResourceId(SITE_ID_RESOURCE_ID) String siteId, String cmisRepoId, String cmisPath, String filename, InputStream content) throws CmisUnavailableException, CmisTimeoutException, CmisRepositoryNotFoundException, CmisPathNotFoundException, ConfigurationException {
DataSourceRepository repositoryConfig = getConfiguration(siteId, cmisRepoId);
CmisUploadItem cmisUploadItem = new CmisUploadItem();
logger.debug("Create new CMIS session");
Session session = createCMISSession(repositoryConfig);
if (session != null) {
String contentPath = Paths.get(repositoryConfig.getBasePath(), cmisPath).toString();
logger.debug("Find object for CMIS path: " + contentPath);
CmisObject cmisObject = session.getObjectByPath(contentPath);
if (cmisObject != null) {
if (BaseTypeId.CMIS_FOLDER.equals(cmisObject.getBaseTypeId())) {
CmisObject docObject = null;
try {
docObject = session.getObjectByPath(Paths.get(contentPath, filename).toString());
} catch (CmisBaseException e) {
// Content does not exist - no error
logger.debug("File " + filename + " does not exist at " + contentPath);
}
MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
String mimeType = mimeTypesMap.getContentType(filename);
ContentStream contentStream = session.getObjectFactory().createContentStream(filename, -1, mimeType, content);
Folder folder = (Folder) cmisObject;
cmisUploadItem.setName(filename);
cmisUploadItem.setFolder(false);
cmisUploadItem.setFileExtension(FilenameUtils.getExtension(filename));
if (docObject != null) {
Document doc = (Document) docObject;
doc.setContentStream(contentStream, true);
String contentId = doc.getId();
StringTokenizer st = new StringTokenizer(contentId, ";");
if (st.hasMoreTokens()) {
cmisUploadItem.setUrl(repositoryConfig.getDownloadUrlRegex().replace(ITEM_ID, st.nextToken()));
}
session.removeObjectFromCache(doc.getId());
} else {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(OBJECT_TYPE_ID, CMIS_DOCUMENT.value());
properties.put(NAME, filename);
Document newDoc = folder.createDocument(properties, contentStream, null);
session.removeObjectFromCache(newDoc.getId());
String contentId = newDoc.getId();
StringTokenizer st = new StringTokenizer(contentId, ";");
if (st.hasMoreTokens()) {
cmisUploadItem.setUrl(repositoryConfig.getDownloadUrlRegex().replace(ITEM_ID, st.nextToken()));
}
}
session.clear();
} else if (CMIS_DOCUMENT.equals(cmisObject.getBaseTypeId())) {
throw new CmisPathNotFoundException();
}
} else {
throw new CmisPathNotFoundException();
}
} else {
throw new CmisUnauthorizedException();
}
return cmisUploadItem;
}
use of org.craftercms.studio.model.rest.CmisUploadItem in project studio by craftercms.
the class CmisController method uploadContent.
@PostMapping(value = "/api/2/cmis/upload")
public ResponseBody uploadContent(HttpServletRequest httpServletRequest) throws IOException, CmisUnavailableException, CmisPathNotFoundException, CmisTimeoutException, CmisRepositoryNotFoundException, FileUploadException, InvalidParametersException, ConfigurationException {
ServletFileUpload servletFileUpload = new ServletFileUpload();
FileItemIterator itemIterator = servletFileUpload.getItemIterator(httpServletRequest);
String filename = StringUtils.EMPTY;
String siteId = StringUtils.EMPTY;
String cmisRepoId = StringUtils.EMPTY;
String cmisPath = StringUtils.EMPTY;
CmisUploadItem cmisUploadItem = new CmisUploadItem();
while (itemIterator.hasNext()) {
FileItemStream item = itemIterator.next();
String name = item.getFieldName();
try (InputStream stream = item.openStream()) {
if (item.isFormField()) {
switch(name) {
case REQUEST_PARAM_SITEID:
siteId = Streams.asString(stream);
break;
case REQUEST_PARAM_CMIS_REPO_ID:
cmisRepoId = Streams.asString(stream);
break;
case REQUEST_PARAM_CMIS_PATH:
cmisPath = Streams.asString(stream);
break;
default:
// Unknown parameter, just skip it...
break;
}
} else {
filename = item.getName();
if (StringUtils.isEmpty(siteId)) {
throw new InvalidParametersException("Invalid siteId");
}
if (StringUtils.isEmpty(cmisRepoId)) {
throw new InvalidParametersException("Invalid cmisRepoId");
}
if (StringUtils.isEmpty(cmisPath)) {
throw new InvalidParametersException("Invalid cmisPath");
}
cmisUploadItem = cmisService.uploadContent(siteId, cmisRepoId, cmisPath, filename, stream);
}
}
}
ResponseBody responseBody = new ResponseBody();
ResultOne<CmisUploadItem> result = new ResultOne<CmisUploadItem>();
result.setResponse(OK);
result.setEntity(RESULT_KEY_ITEM, cmisUploadItem);
responseBody.setResult(result);
return responseBody;
}
Aggregations