try {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// We upload just one file at the same time
MultipartFile uplFile = multipartRequest.getFileMap().entrySet()
.iterator().next().getValue();
CmsUser user = CmsUtils.getUser(request);
int fileSize = (int) (uplFile.getSize() / 1024);
// 文件太大,不允许上传
if (!user.isAllowMaxFile(fileSize)) {
log.warn("member fck upload warn: not allow max file: {}",
fileSize);
return UploadResponse.getFileUploadDisabledError(request);
}
// 文件上传今日额度已经用完
if (!user.isAllowPerDay(fileSize)) {
log.warn("member fck upload warn: not allow per day: {}",
fileSize);
return UploadResponse.getFileUploadDisabledError(request);
}
// Some browsers transfer the entire source path not just the
// filename
String filename = FilenameUtils.getName(uplFile
.getOriginalFilename());
log.debug("Parameter NewFile: {}", filename);
String ext = FilenameUtils.getExtension(filename);
// 不允许上传的文件后缀
if (!user.isAllowSuffix(ext)) {
log.warn("member fck upload warn:"
+ " not allow file extension: {}", ext);
return UploadResponse.getFileUploadDisabledError(request);
}
if (type.isDeniedExtension(ext)) {
return UploadResponse.getInvalidFileTypeError(request);
}
if (type.equals(ResourceType.IMAGE)
&& !ImageUtils.isImage(uplFile.getInputStream())) {
return UploadResponse.getInvalidFileTypeError(request);
}
String fileUrl;
CmsSite site = CmsUtils.getSite(request);
MarkConfig conf = site.getConfig().getMarkConfig();
if (mark == null) {
mark = conf.getOn();
}
boolean isImg = type.equals(ResourceType.IMAGE);
if (site.getConfig().getUploadToDb()) {
if (mark && isImg) {
File tempFile = mark(uplFile, conf);
fileUrl = dbFileMng.storeByExt(site.getUploadPath(), ext,
new FileInputStream(tempFile));
tempFile.delete();
} else {
fileUrl = dbFileMng.storeByExt(site.getUploadPath(), ext,
uplFile.getInputStream());
}
// 加上访问地址
String dbFilePath = site.getConfig().getDbFileUri();
fileUrl = request.getContextPath() + dbFilePath + fileUrl;
} else if (site.getUploadFtp() != null) {
Ftp ftp = site.getUploadFtp();
if (mark && isImg) {
File tempFile = mark(uplFile, conf);
fileUrl = ftp.storeByExt(site.getUploadPath(), ext,
new FileInputStream(tempFile));
tempFile.delete();
} else {
fileUrl = ftp.storeByExt(site.getUploadPath(), ext, uplFile
.getInputStream());
}
// 加上url前缀
fileUrl = ftp.getUrl() + fileUrl;
} else {
if (mark && isImg) {
File tempFile = mark(uplFile, conf);
fileUrl = fileRepository.storeByExt(site.getUploadPath(),
ext, tempFile);
tempFile.delete();
} else {
fileUrl = fileRepository.storeByExt(site.getUploadPath(),
ext, uplFile);
}
// 加上部署路径
fileUrl = request.getContextPath() + fileUrl;
}
cmsUserMng.updateUploadSize(user.getId(), fileSize);
return UploadResponse.getOK(request, fileUrl);
} catch (IOException e) {
return UploadResponse.getFileUploadWriteError(request);
}
}