Using the code cloud (gitee) to make a free chart bed V2.0

effect

preface

The first edition has been completed. This article is based on the first edition. For detailed articles of the first edition, please refer to > >
https://blog.csdn.net/pyfysf/article/details/103990753

https://www.cnblogs.com/upuptop/p/12197125.html

The preview of the first version has been completed. At present, I have developed a free multi person version of the drawing bed, which has been deployed to the server. The server is relatively cheap and the access speed is average. If you want to use the free bed, you can add my wechat (pyfysf{) to get the free account!

Defects in the first version

The logic of V1.0 is to use gitee's gitpage service, upload files, and convert file links to the access address of gitpage service
, defect: because of the use of the gitpage service, you need to refresh the drawing bed (republish the gitpage) after each upload, and there will be frequent operation errors.

solve the problem

V2.0 uses the access mode of resource files to access the warehouse files directly

https://gitee.com/quxuecx/TypechoBlogImg/raw/master/1589128646_20200510124846298_15964.jpg

Specific steps

Create a new open warehouse

After the creation is successful, get your warehouse git address:

This place will be used in subsequent development, please remember

https://gitee.com/apk2sf/TypechoBlogImg.git
apk2sf: User ID
TypechoBlogImg: Warehouse name

No need to open the gitpage service

Create a private token

Start development

Code cloud OpenAPI:
https://gitee.com/api/v5/swagger

We mainly use it here

Parameter list: click the test button below to view the request address

  • Request to establish the giteePages service of pages - > refresh warehouse

Code code

The code basically has no logic, so it's good to request the api of the code cloud through http protocol. Here is the back-end java code sharing

Constant management class

GiteeImgBedConstant.java


/**
 * The constant class of code cloud blog
 *
 * @author: pyfysf
 * <p>
 * @qq: 337081267
 * <p>
 * @CSDN: http://blog.csdn.net/pyfysf
 * <p>
 * @blog: http://wintp.top
 * <p>
 * @email: pyfysf@163.com
 * <p>
 * @time: 2019/12/8
 */
public interface GiteeImgBedConstant {
    /**
     * TODO: This constant is the private Token assigned to you by the code cloud. The code here will report an error just to remind you to modify it
     */
    String ACCESS_TOKEN = 

    /**
     * Warehouse address this is your private user name. Please refer to the precautions when creating the warehouse
     */
    String OWNER = 
    /**
     * TODO: Warehouse name here is your warehouse name
     */
    String REPO_NAME = 
    /**
     * TODO:  Upload the message of the picture
     */
    String CREATE_REPOS_MESSAGE = "add img";
    /**
     * TODO: File prefix 
     */
    String IMG_FILE_DEST_PATH = "/img/" + DateUtil.format(new Date(), "yyyy_MM_dd") + "/";

    /**
     * new file
     * <p>
     * owner*   Address of warehouse space (address path of enterprise, organization or individual)
     * repo*    Warehouse Path
     * path*    Path to file
     * content* File content, base64 encoding
     * message* Submit information
     * <p>
     * %s =>Address of warehouse space (address path of enterprise, organization or individual) (owner)
     * %s => Warehouse Path (repo)
     * %s => Path of file
     */
    String CREATE_REPOS_URL = "https://gitee.com/api/v5/repos/%s/%s/contents/%s";
    /**
     * Request to create a page. If the page is created, it can be refreshed
     * <p>
     * owner*  Address of warehouse space (address path of enterprise, organization or individual)
     * repo*    Warehouse
     */
    String BUILD_PAGE_URL = "https://gitee.com/api/v5/repos/%s/%s/pages/builds";
    /**
     * TODO:  gitpage Request path
     * Example: "https://gitee.com/quxuecx/TypechoBlogImg/raw/master /";
     */
    String GITPAGE_REQUEST_URL = 

}

GiteeBlogImgMController.java


import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import top.wintp.upuptopboot.common.constant.GiteeImgBedConstant;
import top.wintp.upuptopboot.common.utils.ResultUtil;
import top.wintp.upuptopboot.common.vo.Result;

@Slf4j
@RestController
@Api(description = "Management interface of chart bed of code cloud blog")
@RequestMapping("/api/giteeBlogImg")
@Transactional
public class GiteeBlogImgMController {

    @RequestMapping("saveImg")
    @ResponseBody
    public Result<Map<String, Object>> saveImg(@RequestParam(value = "imgFile", required = true) MultipartFile imgFile) throws Exception {
        Result<Map<String, Object>> result = ResultUtil.success("Request succeeded");

        Map<String, Object> resultMap = new HashMap<String, Object>();

        String trueFileName = imgFile.getOriginalFilename();

        assert trueFileName != null;
        String suffix = trueFileName.substring(trueFileName.lastIndexOf("."));

        String fileName = System.currentTimeMillis() + "_" + IdUtil.randomUUID() + suffix;

        String paramImgFile = Base64.encode(imgFile.getBytes());

        //Transfer to gitee
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", GiteeImgBedConstant.ACCESS_TOKEN);
        paramMap.put("message", GiteeImgBedConstant.CREATE_REPOS_MESSAGE);
        paramMap.put("content", paramImgFile);

        String targetDir = GiteeImgBedConstant.IMG_FILE_DEST_PATH + fileName;
        String requestUrl = String.format(GiteeImgBedConstant.CREATE_REPOS_URL, GiteeImgBedConstant.OWNER, 
        GiteeImgBedConstant.REPO_NAME, targetDir);

        System.out.println(requestUrl);

        String resultJson = HttpUtil.post(requestUrl, paramMap);

        JSONObject jsonObject = JSONUtil.parseObj(resultJson);
        if (jsonObject.getObj("commit") != null) {
            String resultImgUrl = GiteeImgBedConstant.GITPAGE_REQUEST_URL + targetDir;
            resultMap.put("resultImgUrl", resultImgUrl);
            System.out.println(resultJson);
            result.setCode(200);
        } else {
            result.setCode(400);
        }
        result.setResult(resultMap);

        return result;
    }

/* 
    There's no need to refresh the drawing bed
    @RequestMapping("refreshPage")
    @ResponseBody
    public Result<Object> refreshPage() throws Exception {
        Result<Object> result = ResultUtil.success("Success');

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("access_token", GiteeImgBedConstant.ACCESS_TOKEN);

        String requestUrl = String.format(GiteeImgBedConstant.BUILD_PAGE_URL,
                GiteeImgBedConstant.OWNER, GiteeImgBedConstant.REPO_NAME);

        System.out.println(requestUrl);

        Map<String, Object> resultMap = new HashMap<>();
        String resultJson = HttpUtil.post(requestUrl, paramMap);

        JSONObject jsonObject = JSONUtil.parseObj(resultJson);
        if (jsonObject.getStr("status") != null) {
            String notice = jsonObject.getStr("notice");
            if (notice != null) {
                if ("Deployed frequently".equalsIgnoreCase(notice)) {
                    resultMap.put("message", "Frequent deployment ');
                    result.setCode(404);
                } else {
                    resultMap.put("message", "Other errors');
                }
                result.setCode(404);

            }
        } else {
            result.setCode(200);
        }

        System.out.println(resultJson);

        return result;
    }

*/

}

Result class:

@Data
public class Result<T> implements Serializable{

    private static final long serialVersionUID = 1L;

    /**
     * Success flag
     */
    private boolean success;

    /**
     * news
     */
    private String message;

    /**
     * Return code
     */
    private Integer code;

    /**
     * time stamp
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * Result object
     */
    private T result;
}

About code Description: the toolkit used in it is in Hutool, and the specific import has been provided in the blog. If you have any problem, please add my friend. QQ: 337081267

The back-end code ends happily

Front end code

V1.0 crossing gate

Consistent with V1.0

Slightly

Upgrade benefits

Access through raw address can eliminate the need for gitpage service and refresh the drawing bed.
An open warehouse and a token can be completed. The service steps of opening gitpage are omitted, and the problem of not refreshing pictures in time is fixed.

If you want to use such a picture bed but don't want to develop it yourself, you can add my wechat and use my platform for free

Hot articles

  1. Uncover the hidden rules of programmer interview, I will tell you quietly
  2. Why do I recommend that you use IDEA instead of Eclipse?
  3. How to prepare technical interview efficiently?

Tags: Java JSON git encoding

Posted on Wed, 13 May 2020 02:05:53 -0700 by warren