Search in sources :

Example 1 with VirtualFile

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;
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) ZhydArticleException(com.zyd.blog.framework.exception.ZhydArticleException) FileUploader(com.zyd.blog.file.FileUploader) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with VirtualFile

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());
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) FileUploader(com.zyd.blog.file.FileUploader) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) PostMapping(org.springframework.web.bind.annotation.PostMapping) BussinessLog(com.zyd.blog.business.annotation.BussinessLog)

Example 3 with VirtualFile

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);
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) FileUploader(com.zyd.blog.file.FileUploader) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) VirtualFile(com.zyd.blog.file.entity.VirtualFile) File(java.io.File) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 4 with 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;
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) FileUploader(com.zyd.blog.file.FileUploader) GlobalFileUploader(com.zyd.blog.plugin.file.GlobalFileUploader) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException) GlobalFileException(com.zyd.blog.file.exception.GlobalFileException)

Example 5 with VirtualFile

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();
            }
        }
    }
}
Also used : VirtualFile(com.zyd.blog.file.entity.VirtualFile) InputStream(java.io.InputStream) OssApiException(com.zyd.blog.file.exception.OssApiException) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

VirtualFile (com.zyd.blog.file.entity.VirtualFile)15 FileUploader (com.zyd.blog.file.FileUploader)6 GlobalFileUploader (com.zyd.blog.plugin.file.GlobalFileUploader)6 GlobalFileException (com.zyd.blog.file.exception.GlobalFileException)4 ApiClient (com.zyd.blog.file.ApiClient)3 Date (java.util.Date)3 BussinessLog (com.zyd.blog.business.annotation.BussinessLog)2 OssApiException (com.zyd.blog.file.exception.OssApiException)2 QiniuApiException (com.zyd.blog.file.exception.QiniuApiException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 QiniuException (com.qiniu.common.QiniuException)1 Response (com.qiniu.http.Response)1 Configuration (com.qiniu.storage.Configuration)1 UploadManager (com.qiniu.storage.UploadManager)1 DefaultPutRet (com.qiniu.storage.model.DefaultPutRet)1 Auth (com.qiniu.util.Auth)1 RedisCache (com.zyd.blog.business.annotation.RedisCache)1