use of com.zyd.blog.file.entity.VirtualFile in project OneBlog by zhangyd-c.
the class BizArticleServiceImpl method publish.
/**
* 发布文章
*
* @param article
* @param tags
* @param file
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public boolean publish(Article article, Long[] tags, MultipartFile file) {
if (null == tags || tags.length <= 0) {
throw new ZhydArticleException("请至少选择一个标签");
}
if (null != file) {
FileUploader uploader = new GlobalFileUploader();
VirtualFile virtualFile = uploader.upload(file, FileUploadType.COVER_IMAGE.getPath(), true);
// 保存封面图片
article.setCoverImage(virtualFile.getFilePath());
}
Long articleId = null;
if ((articleId = article.getId()) != null) {
this.updateSelective(article);
} else {
article.setUserId(SessionUtil.getUser().getId());
articleId = this.insert(article).getId();
}
if (articleId != null) {
articleTagsService.removeByArticleId(articleId);
articleTagsService.insertList(tags, articleId);
}
return true;
}
use of com.zyd.blog.file.entity.VirtualFile in project OneBlog by zhangyd-c.
the class RestApiController method uploadFile.
@BussinessLog("wangEditor编辑器中上传文件")
@RequiresPermissions("article:publish")
@PostMapping("/uploadFile")
public ResponseVO uploadFile(@RequestParam("file") MultipartFile file) {
FileUploader uploader = new GlobalFileUploader();
VirtualFile virtualFile = uploader.upload(file, FileUploadType.SIMPLE.getPath(), true);
return ResultUtil.success("图片上传成功", virtualFile.getFullFilePath());
}
use of com.zyd.blog.file.entity.VirtualFile in project OneBlog by zhangyd-c.
the class BlogAdminApplicationTests method uploadFile.
@Test
public void uploadFile() {
FileUploader uploader = new GlobalFileUploader();
File file = new File("C:\\Users\\yadon\\Desktop\\新建文件夹\\web-index-pc.png");
VirtualFile virtualFile = uploader.upload(file, FileUploadType.SIMPLE.getPath(), true);
System.out.println(virtualFile);
}
use of com.zyd.blog.file.entity.VirtualFile in project OneBlog by zhangyd-c.
the class ImageDownloadUtil method saveToCloudStorage.
/**
* 将网络图片转存到云存储中
*
* @param imgUrl 网络图片地址
* @param referer 为了预防某些网站做了权限验证,不加referer可能会403
*/
public static String saveToCloudStorage(String imgUrl, String referer) {
log.debug("download img >> %s", imgUrl);
String res = null;
try (InputStream is = getInputStreamByUrl(imgUrl, referer)) {
FileUploader uploader = new GlobalFileUploader();
VirtualFile file = uploader.upload(is, FileUploadType.SIMPLE.getPath(), imgUrl, false);
res = file.getFullFilePath();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new GlobalFileException(e.getMessage());
}
return res;
}
use of com.zyd.blog.file.entity.VirtualFile in project OneBlog by zhangyd-c.
the class AliyunOssApiClient method uploadImg.
@Override
public VirtualFile uploadImg(InputStream is, String imageUrl) {
this.check();
String key = FileUtil.generateTempFileName(imageUrl);
this.createNewFileName(key, this.pathPrefix);
Date startTime = new Date();
try (InputStream uploadIs = StreamUtil.clone(is);
InputStream fileHashIs = StreamUtil.clone(is)) {
ossApi.uploadFile(uploadIs, this.newFileName, bucketName);
return new VirtualFile().setOriginalFileName(FileUtil.getName(key)).setSuffix(this.suffix).setUploadStartTime(startTime).setUploadEndTime(new Date()).setFilePath(this.newFileName).setFileHash(DigestUtils.md5DigestAsHex(fileHashIs)).setFullFilePath(this.url + this.newFileName);
} catch (IOException e) {
throw new OssApiException("[" + this.storageType + "]文件上传失败:" + e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Aggregations