使用碼雲(gitee)碼雲作一個免費圖牀V2.0

使用碼雲(gitee)碼雲作一個免費圖牀V2.0

效果

1

前言

第一版已完成,這篇文章是在第一版的基礎上進行改造升級了。第一版的詳細文章請見>>>
https://blog.csdn.net/pyfysf/article/details/103990753

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

第一版完成的預覽圖,目前我這裏已經開發好了免費圖牀多人版本,並且已經部署到服務器上了,服務器比較廉價,訪問速度一般。如果想使用免費圖牀的朋友們,可以添加我微信(pyfysf_123)領取免費賬號喲!

使用碼雲(gitee)碼雲作一個免費圖牀V2.0

第一個版本的缺陷

V1.0 的做法邏輯是 利用gitee的gitpage服務,通過上傳文件,將文件鏈接轉換爲gitpage服務的訪問地址
缺陷:因爲是使用了gitpage服務,所以每次上傳完成之後都需要刷新圖牀操作(重新發布gitpage),還會出現頻繁操作的錯誤。

解決問題

V2.0使用了資源文件的訪問方式操作直接對倉庫的文件進行raw訪問

使用碼雲(gitee)碼雲作一個免費圖牀V2.0

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

具體步驟

創建一個新的公開倉庫

format,png

創建成功之後,獲取你的倉庫git地址:

這個地方後續開發中會用到,一定要記得哦

https://gitee.com/apk2sf/TypechoBlogImg.git
apk2sf: 用戶標識
TypechoBlogImg: 倉庫名稱

不需要開啓gitpage服務

創建私人令牌

format,png

開始開發

碼雲OpenAPI :
https://gitee.com/api/v5/swagger

我們這裏主要使用到了

參數列表:點擊下方的測試按鈕,可以查看到請求地址

使用碼雲(gitee)碼雲作一個免費圖牀V2.0

  • 請求建立Pages --> 刷新倉庫的giteePages服務

使用碼雲(gitee)碼雲作一個免費圖牀V2.0

碼代碼

代碼基本上沒有什麼邏輯,通過http協議請求碼雲的api就好了。下面是後端java代碼分享

常量管理類

GiteeImgBedConstant.java


/**
 * 碼雲博客圖牀的常量類
 *
 * @author: pyfysf
 * <p>
 * @qq: 337081267
 * <p>
 * @CSDN: http://blog.csdn.net/pyfysf
 * <p>
 * @blog: http://wintp.top
 * <p>
 * @email: [email protected]
 * <p>
 * @time: 2019/12/8
 */
public interface GiteeImgBedConstant {
    /**
     * TODO:這個常量是碼云爲您分配的私人令牌,Token  這裏的代碼會報錯,僅僅是爲了提醒您進行修改
     */
    String ACCESS_TOKEN = 

    /**
     * 倉庫所屬地址  這個是您的私人用戶名 具體請參考創建倉庫時的注意事項
     */
    String OWNER = 
    /**
     * TODO:倉庫名稱  這裏是您的倉庫名稱
     */
    String REPO_NAME = 
    /**
     * TODO: 上傳圖片的message
     */
    String CREATE_REPOS_MESSAGE = "add img";
    /**
     * TODO:文件前綴
     */
    String IMG_FILE_DEST_PATH = "/img/" + DateUtil.format(new Date(), "yyyy_MM_dd") + "/";

    /**
     * 新建文件
     * <p>
     * owner*   倉庫所屬空間地址(企業、組織或個人的地址path)
     * repo*    倉庫路徑
     * path*    文件的路徑
     * content* 文件內容, 要用 base64 編碼
     * message* 提交信息
     * <p>
     * %s =>倉庫所屬空間地址(企業、組織或個人的地址path)  (owner)
     * %s => 倉庫路徑(repo)
     * %s => 文件的路徑(path)
     */
    String CREATE_REPOS_URL = "https://gitee.com/api/v5/repos/%s/%s/contents/%s";
    /**
     * 請求建立page  如果建立了,可以刷新
     * <p>
     * owner*  倉庫所屬空間地址(企業、組織或個人的地址path)
     * repo*    倉庫
     */
    String BUILD_PAGE_URL = "https://gitee.com/api/v5/repos/%s/%s/pages/builds";
    /**
     * TODO: gitpage請求路徑
     * 示例:"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 = "碼雲博客圖牀管理接口")
@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("請求成功");

        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());

        //轉存到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;
    }

/* 
    這裏不需要刷新圖牀了
    @RequestMapping("refreshPage")
    @ResponseBody
    public Result<Object> refreshPage() throws Exception {
        Result<Object> result = ResultUtil.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", "部署頻繁");
                    result.setCode(404);
                } else {
                    resultMap.put("message", "其他錯誤");
                }
                result.setCode(404);

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

        System.out.println(resultJson);

        return result;
    }

*/

}

Result類:

@Data
public class Result<T> implements Serializable{

    private static final long serialVersionUID = 1L;

    /**
     * 成功標誌
     */
    private boolean success;

    /**
     * 消息
     */
    private String message;

    /**
     * 返回代碼
     */
    private Integer code;

    /**
     * 時間戳
     */
    private long timestamp = System.currentTimeMillis();

    /**
     * 結果對象
     */
    private T result;
}

關於代碼說明:裏面所用到的工具包在Hutool中,具體的import已在博文中提供。如遇到問題,歡迎加我好友哦~ QQ:337081267

後端代碼就這樣愉快的結束了……

前端代碼

V1.0穿越門

與V1.0一致

略……

升級優點

通過raw的地址進行訪問,就可以不需要gitpage服務了,也不需要重新刷新圖牀了。
一個公開的倉庫,一個token 既可完成。省略了開通gitpage的服務步驟,修復了無法及時刷新圖片的問題。

495398f7c5b02cac53f15930d04c686

如果你想使用這樣一個圖牀,但是又不想自己開發,你可以添加我的微信,可以免費使用我的平臺……

熱點文章

  1. 揭祕程序員面試潛規則,我悄悄告訴你
  2. 爲什麼我推薦你使用 IDEA 而不是Eclipse ?
  3. 如何高效地準備技術面試?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章