use of com.moxi.mogublog.commons.entity.FileSort in project mogu_blog_v2 by moxi624.
the class FileServiceImpl method batchUploadFile.
@Override
public String batchUploadFile(HttpServletRequest request, List<MultipartFile> filedatas, SystemConfig systemConfig) {
String uploadQiNiu = systemConfig.getUploadQiNiu();
String uploadLocal = systemConfig.getUploadLocal();
String uploadMinio = systemConfig.getUploadMinio();
// 判断来源
String source = request.getParameter(SysConf.SOURCE);
// 如果是用户上传,则包含用户uid
String userUid = "";
// 如果是管理员上传,则包含管理员uid
String adminUid = "";
// 项目名
String projectName = "";
// 模块名
String sortName = "";
// 判断图片来源
if (SysConf.PICTURE.equals(source)) {
// 当从vue-mogu-web网站过来的,直接从参数中获取
userUid = request.getParameter(SysConf.USER_UID);
adminUid = request.getParameter(SysConf.ADMIN_UID);
projectName = request.getParameter(SysConf.PROJECT_NAME);
sortName = request.getParameter(SysConf.SORT_NAME);
} else if (SysConf.ADMIN.equals(source)) {
// 当图片从mogu-admin传递过来的时候
userUid = request.getAttribute(SysConf.USER_UID).toString();
adminUid = request.getAttribute(SysConf.ADMIN_UID).toString();
projectName = request.getAttribute(SysConf.PROJECT_NAME).toString();
sortName = request.getAttribute(SysConf.SORT_NAME).toString();
} else {
userUid = request.getAttribute(SysConf.USER_UID).toString();
adminUid = request.getAttribute(SysConf.ADMIN_UID).toString();
projectName = request.getAttribute(SysConf.PROJECT_NAME).toString();
sortName = request.getAttribute(SysConf.SORT_NAME).toString();
}
// projectName现在默认base
if (StringUtils.isEmpty(projectName)) {
projectName = "base";
}
// TODO 检测用户上传,如果不是网站的用户就不能调用
if (StringUtils.isEmpty(userUid) && StringUtils.isEmpty(adminUid)) {
return ResultUtil.result(SysConf.ERROR, "请先注册");
}
QueryWrapper<FileSort> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(SQLConf.SORT_NAME, sortName);
queryWrapper.eq(SQLConf.PROJECT_NAME, projectName);
queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
List<FileSort> fileSorts = fileSortService.list(queryWrapper);
FileSort fileSort = null;
if (fileSorts.size() >= 1) {
fileSort = fileSorts.get(0);
} else {
return ResultUtil.result(SysConf.ERROR, "文件不被允许上传");
}
String sortUrl = fileSort.getUrl();
// 判断url是否为空,如果为空,使用默认
if (StringUtils.isEmpty(sortUrl)) {
sortUrl = "base/common/";
} else {
sortUrl = fileSort.getUrl();
}
List<File> lists = new ArrayList<>();
// 文件上传
if (filedatas != null && filedatas.size() > 0) {
for (MultipartFile filedata : filedatas) {
String oldName = filedata.getOriginalFilename();
long size = filedata.getSize();
// 获取扩展名,默认是jpg
String picExpandedName = FileUtils.getPicExpandedName(oldName);
// 获取新文件名
String newFileName = System.currentTimeMillis() + Constants.SYMBOL_POINT + picExpandedName;
String localUrl = "";
String qiNiuUrl = "";
String minioUrl = "";
try {
MultipartFile tempFileData = filedata;
// 上传七牛云,判断是否能够上传七牛云
if (EOpenStatus.OPEN.equals(uploadQiNiu)) {
qiNiuUrl = qiniuService.uploadFile(tempFileData);
}
// 判断是否能够上传Minio文件服务器
if (EOpenStatus.OPEN.equals(uploadMinio)) {
minioUrl = minioService.uploadFile(tempFileData);
}
// 判断是否能够上传至本地
if (EOpenStatus.OPEN.equals(uploadLocal)) {
localUrl = localFileService.uploadFile(filedata, fileSort);
}
} catch (Exception e) {
log.info("上传文件异常: {}", e.getMessage());
e.getStackTrace();
return ResultUtil.result(SysConf.ERROR, "文件上传失败,请检查系统配置");
}
File file = new File();
file.setCreateTime(new Date(System.currentTimeMillis()));
file.setFileSortUid(fileSort.getUid());
file.setFileOldName(oldName);
file.setFileSize(size);
file.setPicExpandedName(picExpandedName);
file.setPicName(newFileName);
file.setPicUrl(localUrl);
file.setStatus(EStatus.ENABLE);
file.setUserUid(userUid);
file.setAdminUid(adminUid);
file.setQiNiuUrl(qiNiuUrl);
file.setMinioUrl(minioUrl);
file.insert();
lists.add(file);
}
// 保存成功返回数据
return ResultUtil.result(SysConf.SUCCESS, lists);
}
return ResultUtil.result(SysConf.ERROR, "请上传图片");
}
use of com.moxi.mogublog.commons.entity.FileSort in project mogu_blog_v2 by moxi624.
the class FileServiceImpl method ckeditorUploadCopyFile.
@Override
public Object ckeditorUploadCopyFile() {
HttpServletRequest request = RequestHolder.getRequest();
// 从参数中获取token【该方法用于ckeditor复制图片上传,所以会携带token在参数中】
String token = request.getParameter(SysConf.TOKEN);
if (StringUtils.isEmpty(token)) {
throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, "未读取到携带token");
}
String[] params = token.split("\\?url=");
// 从Redis中获取系统配置文件
Map<String, String> qiNiuConfig = new HashMap<>();
Map<String, String> resultMap = feignUtil.getSystemConfigMap(params[0]);
SystemConfig systemConfig = feignUtil.getSystemConfigByMap(resultMap);
String userUid = "uid00000000000000000000000000000000";
String adminUid = "uid00000000000000000000000000000000";
String projectName = "blog";
String sortName = "admin";
// 需要上传的URL
String itemUrl = params[1];
// 判断需要上传的域名和本机图片域名是否一致
if (EFilePriority.QI_NIU.equals(systemConfig.getContentPicturePriority())) {
// 判断需要上传的域名和本机图片域名是否一致,如果一致,那么就不需要重新上传,而是直接返回
if (StringUtils.isNotEmpty(systemConfig.getQiNiuPictureBaseUrl()) && StringUtils.isNotEmpty(itemUrl) && itemUrl.indexOf(systemConfig.getQiNiuPictureBaseUrl()) > -1) {
Map<String, Object> result = new HashMap<>();
result.put(SysConf.UPLOADED, 1);
result.put(SysConf.FILE_NAME, itemUrl);
result.put(SysConf.URL, itemUrl);
return result;
}
} else if (EFilePriority.MINIO.equals(systemConfig.getContentPicturePriority())) {
// 判断需要上传的域名和本机图片域名是否一致,如果一致,那么就不需要重新上传,而是直接返回
if (StringUtils.isNotEmpty(systemConfig.getMinioPictureBaseUrl()) && StringUtils.isNotEmpty(itemUrl) && itemUrl.indexOf(systemConfig.getMinioPictureBaseUrl()) > -1) {
Map<String, Object> result = new HashMap<>();
result.put(SysConf.UPLOADED, 1);
result.put(SysConf.FILE_NAME, itemUrl);
result.put(SysConf.URL, itemUrl);
return result;
}
} else {
// 判断需要上传的域名和本机图片域名是否一致,如果一致,那么就不需要重新上传,而是直接返回
if (StringUtils.isNotEmpty(systemConfig.getLocalPictureBaseUrl()) && StringUtils.isNotEmpty(itemUrl) && itemUrl.indexOf(systemConfig.getLocalPictureBaseUrl()) > -1) {
Map<String, Object> result = new HashMap<>();
result.put(SysConf.UPLOADED, 1);
result.put(SysConf.FILE_NAME, itemUrl);
result.put(SysConf.URL, itemUrl);
return result;
}
}
// projectName现在默认base
if (StringUtils.isEmpty(projectName)) {
projectName = "base";
}
// TODO 这里可以检测用户上传,如果不是网站的用户或会员就不能调用
if (StringUtils.isEmpty(userUid) && StringUtils.isEmpty(adminUid)) {
return ResultUtil.result(SysConf.ERROR, "请先注册");
}
QueryWrapper<FileSort> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(SQLConf.SORT_NAME, sortName);
queryWrapper.eq(SQLConf.PROJECT_NAME, projectName);
queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
List<FileSort> fileSorts = fileSortService.list(queryWrapper);
FileSort fileSort = null;
if (fileSorts.size() > 0) {
fileSort = fileSorts.get(0);
} else {
return ResultUtil.result(SysConf.ERROR, "文件不被允许上传");
}
String sortUrl = fileSort.getUrl();
// 判断url是否为空,如果为空,使用默认
if (StringUtils.isEmpty(sortUrl)) {
sortUrl = "base/common/";
} else {
sortUrl = fileSort.getUrl();
}
// 获取新文件名(默认为jpg)
String newFileName = System.currentTimeMillis() + ".jpg";
// 文件url访问地址
String localUrl = "";
String qiNiuUrl = "";
String minioUrl = "";
// 上传到本地服务器【判断是否能够上传至本地】
if (EOpenStatus.OPEN.equals(systemConfig.getUploadLocal())) {
localUrl = localFileService.uploadPictureByUrl(itemUrl, fileSort);
}
// 上传七牛云 【判断是否能够上传七牛云】
if (EOpenStatus.OPEN.equals(systemConfig.getUploadMinio())) {
minioUrl = minioService.uploadPictureByUrl(itemUrl);
}
// 上传七牛云 【判断是否能够上传七牛云】
if (EOpenStatus.OPEN.equals(systemConfig.getUploadQiNiu())) {
qiNiuUrl = qiniuService.uploadPictureByUrl(itemUrl, systemConfig);
}
File file = new File();
file.setCreateTime(new Date(System.currentTimeMillis()));
file.setFileSortUid(fileSort.getUid());
file.setFileOldName(itemUrl);
file.setFileSize(0L);
file.setPicExpandedName(Constants.FILE_SUFFIX_JPG);
file.setPicName(newFileName);
// 设置本地图片
file.setPicUrl(systemConfig.getLocalPictureBaseUrl() + localUrl);
// 设置minio图片
file.setMinioUrl(systemConfig.getMinioPictureBaseUrl() + minioUrl);
// 设置七牛云图片
file.setQiNiuUrl(systemConfig.getQiNiuPictureBaseUrl() + qiNiuUrl);
file.setStatus(EStatus.ENABLE);
file.setUserUid(userUid);
file.setAdminUid(adminUid);
fileService.save(file);
Map<String, Object> result = new HashMap<>();
result.put(SysConf.UPLOADED, 1);
result.put(SysConf.FILE_NAME, newFileName);
// 设置显示方式
if (EFilePriority.QI_NIU.equals(qiNiuConfig.get(SysConf.PICTURE_PRIORITY))) {
result.put(SysConf.URL, systemConfig.getQiNiuPictureBaseUrl() + qiNiuUrl);
} else if (EFilePriority.MINIO.equals(qiNiuConfig.get(SysConf.PICTURE_PRIORITY))) {
result.put(SysConf.URL, systemConfig.getMinioPictureBaseUrl() + localUrl);
} else {
result.put(SysConf.URL, systemConfig.getLocalPictureBaseUrl() + localUrl);
}
return result;
}
use of com.moxi.mogublog.commons.entity.FileSort in project mogu_blog_v2 by moxi624.
the class FileServiceImpl method uploadPictureByUrl.
@Override
public String uploadPictureByUrl(FileVO fileVO) {
// 获取配置文件
SystemConfig systemConfig;
if (fileVO.getSystemConfig() != null) {
Map<String, String> resultMap = fileVO.getSystemConfig();
systemConfig = feignUtil.getSystemConfigByMap(resultMap);
} else {
// 从Redis中获取七牛云配置文件
systemConfig = feignUtil.getSystemConfig();
}
String userUid = fileVO.getUserUid();
String adminUid = fileVO.getAdminUid();
String projectName = fileVO.getProjectName();
String sortName = fileVO.getSortName();
List<String> urlList = fileVO.getUrlList();
// projectName现在默认base
if (StringUtils.isEmpty(projectName)) {
projectName = "base";
}
// 这里可以检测用户上传,如果不是网站的用户或会员就不能调用
if (StringUtils.isEmpty(userUid) && StringUtils.isEmpty(adminUid)) {
throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, "请先注册");
}
QueryWrapper<FileSort> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(SQLConf.SORT_NAME, sortName);
queryWrapper.eq(SQLConf.PROJECT_NAME, projectName);
queryWrapper.eq(SQLConf.STATUS, EStatus.ENABLE);
List<FileSort> fileSorts = fileSortService.list(queryWrapper);
FileSort fileSort = null;
if (fileSorts.size() > 0) {
fileSort = fileSorts.get(0);
} else {
throw new InsertException(ErrorCode.INSERT_DEFAULT_ERROR, "文件不被允许上传, 请填写文件分类信息");
}
List<com.moxi.mogublog.commons.entity.File> lists = new ArrayList<>();
// 文件上传
if (urlList != null && urlList.size() > 0) {
for (String itemUrl : urlList) {
// 获取新文件名(默认为jpg)
String newFileName = System.currentTimeMillis() + ".jpg";
// 将图片上传到本地服务器中以及七牛云中
String picurl = "";
String qiNiuUrl = "";
String minioUrl = "";
// 判断是否能够上传至本地
if (EOpenStatus.OPEN.equals(systemConfig.getUploadLocal())) {
picurl = localFileService.uploadPictureByUrl(itemUrl, fileSort);
}
// 上传七牛云,判断是否能够上传七牛云
if (EOpenStatus.OPEN.equals(systemConfig.getUploadMinio())) {
minioUrl = minioService.uploadPictureByUrl(itemUrl);
}
// 上传七牛云,判断是否能够上传七牛云
if (EOpenStatus.OPEN.equals(systemConfig.getUploadQiNiu())) {
qiNiuUrl = qiniuService.uploadPictureByUrl(itemUrl, systemConfig);
}
com.moxi.mogublog.commons.entity.File file = new com.moxi.mogublog.commons.entity.File();
file.setCreateTime(new Date(System.currentTimeMillis()));
file.setFileSortUid(fileSort.getUid());
file.setFileOldName(itemUrl);
file.setFileSize(0L);
file.setPicExpandedName("jpg");
file.setPicName(newFileName);
file.setPicUrl(picurl);
file.setQiNiuUrl(qiNiuUrl);
file.setMinioUrl(minioUrl);
file.setStatus(EStatus.ENABLE);
file.setUserUid(userUid);
file.setAdminUid(adminUid);
fileService.save(file);
lists.add(file);
}
// 保存成功返回数据
return ResultUtil.result(SysConf.SUCCESS, lists);
}
return ResultUtil.result(SysConf.ERROR, "请上传图片");
}
Aggregations