mirror of
https://github.com/doocs/md.git
synced 2024-11-24 19:10:34 +08:00
refactor: update file api
This commit is contained in:
parent
d97b153a58
commit
04f39f5de7
232
src/api/file.js
232
src/api/file.js
@ -7,66 +7,13 @@ import { v4 as uuidv4 } from "uuid";
|
|||||||
import * as qiniu from "qiniu-js";
|
import * as qiniu from "qiniu-js";
|
||||||
import { utf16to8, base64encode, safe64 } from "../assets/scripts/tokenTools";
|
import { utf16to8, base64encode, safe64 } from "../assets/scripts/tokenTools";
|
||||||
|
|
||||||
function fileUpload(content, file) {
|
|
||||||
const imgHost = localStorage.getItem("imgHost");
|
|
||||||
!imgHost && localStorage.setItem("imgHost", "default");
|
|
||||||
switch (imgHost) {
|
|
||||||
case "aliOSS":
|
|
||||||
return aliOSSFileUpload(content, file.name);
|
|
||||||
case "txCOS":
|
|
||||||
return txCOSFileUpload(file);
|
|
||||||
case "qiniu":
|
|
||||||
return qiniuUpload(file);
|
|
||||||
case "gitee":
|
|
||||||
return giteeUpload(content, file.name);
|
|
||||||
case "github":
|
|
||||||
return ghFileUpload(content, file.name);
|
|
||||||
default:
|
|
||||||
// return file.size / 1024 < 1024
|
|
||||||
// ? giteeUpload(content, file.name)
|
|
||||||
// : ghFileUpload(content, file.name);
|
|
||||||
return ghFileUpload(content, file.name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDir() {
|
|
||||||
const date = new Date();
|
|
||||||
const year = date.getFullYear();
|
|
||||||
const month = (date.getMonth() + 1).toString().padStart(2, "0");
|
|
||||||
const day = date.getDate().toString().padStart(2, "0");
|
|
||||||
return `${year}/${month}/${day}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDateFilename(filename) {
|
|
||||||
const currentTimestamp = new Date().getTime();
|
|
||||||
const fileSuffix = filename.split(".")[1];
|
|
||||||
return `${currentTimestamp}-${uuidv4()}.${fileSuffix}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
// GitHub File Upload
|
// GitHub File Upload
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
|
||||||
function getGitHubCommonConfig(username, repo, branch, token) {
|
function getGitHubConfig(useDefault) {
|
||||||
const dir = getDir();
|
if (useDefault) {
|
||||||
return {
|
const accessToken = [
|
||||||
method: "put",
|
|
||||||
headers: {
|
|
||||||
Authorization: "token " + token,
|
|
||||||
},
|
|
||||||
username: username,
|
|
||||||
repo: repo,
|
|
||||||
branch: branch,
|
|
||||||
url: `https://api.github.com/repos/${username}/${repo}/contents/${dir}/`,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function getGitHubDefaultConfig() {
|
|
||||||
const defaultConfig = {
|
|
||||||
username: "filess",
|
|
||||||
repo: `img${Math.floor(Math.random() * 10)}`,
|
|
||||||
branch: "main",
|
|
||||||
accessToken: [
|
|
||||||
"7715d7ca67b5d3837cfdoocsmde8c38421815aa423510af",
|
"7715d7ca67b5d3837cfdoocsmde8c38421815aa423510af",
|
||||||
"c411415bf95dbe39625doocsmd5047ba9b7a2a6c9642abe",
|
"c411415bf95dbe39625doocsmd5047ba9b7a2a6c9642abe",
|
||||||
"2821cd8819fa345c053doocsmdca86ac653f8bc20db1f1b",
|
"2821cd8819fa345c053doocsmdca86ac653f8bc20db1f1b",
|
||||||
@ -75,58 +22,53 @@ function getGitHubDefaultConfig() {
|
|||||||
"b67e9d15cb6f910492fdoocsmdac6b44d379c953bb19eff",
|
"b67e9d15cb6f910492fdoocsmdac6b44d379c953bb19eff",
|
||||||
"618c4dc2244ccbbc088doocsmd125d17fd31b7d06a50cf3",
|
"618c4dc2244ccbbc088doocsmd125d17fd31b7d06a50cf3",
|
||||||
"a4b581732e1c1507458doocsmdc5b223b27dae5e2e16a55",
|
"a4b581732e1c1507458doocsmdc5b223b27dae5e2e16a55",
|
||||||
],
|
];
|
||||||
};
|
const randIndex = Math.floor(Math.random() * accessToken.length);
|
||||||
|
const token = accessToken[randIndex].replace("doocsmd", "");
|
||||||
const randIndex = Math.floor(
|
return {
|
||||||
Math.random() * defaultConfig.accessToken.length
|
username: "filess",
|
||||||
);
|
// img0...img9
|
||||||
const token = defaultConfig.accessToken[randIndex].replace("doocsmd", "");
|
repo: `img${Math.floor(Math.random() * 10)}`,
|
||||||
return getGitHubCommonConfig(
|
branch: "main",
|
||||||
defaultConfig.username,
|
accessToken: token,
|
||||||
defaultConfig.repo,
|
};
|
||||||
defaultConfig.branch,
|
}
|
||||||
token
|
const customConfig = JSON.parse(localStorage.getItem("githubConfig"));
|
||||||
);
|
const repoUrl = customConfig.repo
|
||||||
}
|
|
||||||
|
|
||||||
function getGitHubConfig() {
|
|
||||||
const githubConfig = JSON.parse(localStorage.getItem("githubConfig"));
|
|
||||||
const repoUrl = githubConfig.repo
|
|
||||||
.replace("https://github.com/", "")
|
.replace("https://github.com/", "")
|
||||||
.replace("http://github.com/", "")
|
.replace("http://github.com/", "")
|
||||||
.replace("github.com/", "")
|
.replace("github.com/", "")
|
||||||
.split("/");
|
.split("/");
|
||||||
const username = repoUrl[0];
|
return {
|
||||||
const repo = repoUrl[1];
|
username: repoUrl[0],
|
||||||
return getGitHubCommonConfig(
|
repo: repoUrl[1],
|
||||||
username,
|
branch: customConfig.branch || "master",
|
||||||
repo,
|
accessToken: customConfig.accessToken,
|
||||||
githubConfig.branch,
|
};
|
||||||
githubConfig.accessToken
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function ghFileUpload(content, filename) {
|
async function ghFileUpload(content, filename) {
|
||||||
const isDefault = localStorage.getItem("imgHost") !== "github";
|
const useDefault = localStorage.getItem("imgHost") === "default";
|
||||||
const config = isDefault ? getGitHubDefaultConfig() : getGitHubConfig();
|
const config = getGitHubConfig(useDefault);
|
||||||
|
const dir = getDir();
|
||||||
|
const url = `https://api.github.com/repos/${config.username}/${config.repo}/contents/${dir}/`;
|
||||||
const dateFilename = getDateFilename(filename);
|
const dateFilename = getDateFilename(filename);
|
||||||
|
|
||||||
const branch = config.branch || "master";
|
|
||||||
const res = await fetch({
|
const res = await fetch({
|
||||||
url: config.url + dateFilename,
|
url: url + dateFilename,
|
||||||
method: config.method,
|
method: "put",
|
||||||
headers: config.headers,
|
headers: {
|
||||||
|
Authorization: `token ${config.accessToken}`,
|
||||||
|
},
|
||||||
data: {
|
data: {
|
||||||
branch: branch,
|
branch: config.branch,
|
||||||
message: `Upload by ${window.location.href}`,
|
message: `Upload by ${window.location.href}`,
|
||||||
content: content,
|
content: content,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const githubResourceUrl = `raw.githubusercontent.com/${config.username}/${config.repo}/${branch}/`;
|
const githubResourceUrl = `raw.githubusercontent.com/${config.username}/${config.repo}/${config.branch}/`;
|
||||||
const cdnResourceUrl = `cdn.jsdelivr.net/gh/${config.username}/${config.repo}@${branch}/`;
|
const cdnResourceUrl = `cdn.jsdelivr.net/gh/${config.username}/${config.repo}@${config.branch}/`;
|
||||||
return isDefault
|
return useDefault
|
||||||
? res.content.download_url.replace(githubResourceUrl, cdnResourceUrl)
|
? res.content.download_url.replace(githubResourceUrl, cdnResourceUrl)
|
||||||
: res.content.download_url;
|
: res.content.download_url;
|
||||||
}
|
}
|
||||||
@ -135,12 +77,9 @@ async function ghFileUpload(content, filename) {
|
|||||||
// Gitee File Upload
|
// Gitee File Upload
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
|
||||||
function getGiteeDefaultConfig() {
|
function getGiteeConfig(useDefault) {
|
||||||
const defaultConfig = {
|
if (useDefault) {
|
||||||
username: "filesss",
|
const accessToken = [
|
||||||
repo: `img${Math.floor(Math.random() * 10)}`,
|
|
||||||
branch: "main",
|
|
||||||
accessToken: [
|
|
||||||
"ed5fc9866bd6c2fdoocsmddd433f806fd2f399c",
|
"ed5fc9866bd6c2fdoocsmddd433f806fd2f399c",
|
||||||
"5448ffebbbf1151doocsmdc4e337cf814fc8a62",
|
"5448ffebbbf1151doocsmdc4e337cf814fc8a62",
|
||||||
"25b05efd2557ca2doocsmd75b5c0835e3395911",
|
"25b05efd2557ca2doocsmd75b5c0835e3395911",
|
||||||
@ -149,40 +88,45 @@ function getGiteeDefaultConfig() {
|
|||||||
"d8c0b57500672c1doocsmd55f48b866b5ebcd98",
|
"d8c0b57500672c1doocsmd55f48b866b5ebcd98",
|
||||||
"78c56eadb88e453doocsmd43ddd95753351771a",
|
"78c56eadb88e453doocsmd43ddd95753351771a",
|
||||||
"03e1a688003948fdoocsmda16fcf41e6f03f1f0",
|
"03e1a688003948fdoocsmda16fcf41e6f03f1f0",
|
||||||
],
|
];
|
||||||
};
|
const randIndex = Math.floor(Math.random() * accessToken.length);
|
||||||
|
const token = accessToken[randIndex].replace("doocsmd", "");
|
||||||
const randIndex = Math.floor(
|
return {
|
||||||
Math.random() * defaultConfig.accessToken.length
|
username: "filesss",
|
||||||
);
|
// img0...img9
|
||||||
const token = defaultConfig.accessToken[randIndex].replace("doocsmd", "");
|
repo: `img${Math.floor(Math.random() * 10)}`,
|
||||||
return {
|
branch: "main",
|
||||||
repo: `gitee.com/${defaultConfig.username}/${defaultConfig.repo}`,
|
accessToken: token,
|
||||||
branch: defaultConfig.branch,
|
};
|
||||||
accessToken: token,
|
}
|
||||||
};
|
const customConfig = JSON.parse(localStorage.getItem("giteeConfig"));
|
||||||
}
|
const repoUrl = customConfig.repo
|
||||||
|
|
||||||
async function giteeUpload(content, filename) {
|
|
||||||
const isDefault = localStorage.getItem("imgHost") == "default";
|
|
||||||
const giteeConfig = isDefault
|
|
||||||
? getGiteeDefaultConfig()
|
|
||||||
: JSON.parse(localStorage.getItem("giteeConfig"));
|
|
||||||
const repoUrl = giteeConfig.repo
|
|
||||||
.replace("https://gitee.com/", "")
|
.replace("https://gitee.com/", "")
|
||||||
.replace("http://gitee.com/", "")
|
.replace("http://gitee.com/", "")
|
||||||
.replace("gitee.com/", "")
|
.replace("gitee.com/", "")
|
||||||
.split("/");
|
.split("/");
|
||||||
const username = repoUrl[0];
|
return {
|
||||||
const repo = repoUrl[1];
|
username: repoUrl[0],
|
||||||
|
repo: repoUrl[1],
|
||||||
|
branch: customConfig.branch || "master",
|
||||||
|
accessToken: customConfig.accessToken,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function giteeUpload(content, filename) {
|
||||||
|
const useDefault = JSON.parse(
|
||||||
|
localStorage.getItem("imgHost") === "default"
|
||||||
|
);
|
||||||
|
const config = getGiteeConfig(useDefault);
|
||||||
const dir = getDir();
|
const dir = getDir();
|
||||||
const dateFilename = getDateFilename(filename);
|
const dateFilename = getDateFilename(filename);
|
||||||
|
const url = `https://gitee.com/api/v5/repos/${config.username}/${config.repo}/contents/${dir}/${dateFilename}`;
|
||||||
const res = await fetch({
|
const res = await fetch({
|
||||||
url: `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${dir}/${dateFilename}`,
|
url,
|
||||||
method: "POST",
|
method: "POST",
|
||||||
data: {
|
data: {
|
||||||
access_token: giteeConfig.accessToken,
|
access_token: config.accessToken,
|
||||||
branch: giteeConfig.branch || "master",
|
branch: config.branch,
|
||||||
content: content,
|
content: content,
|
||||||
message: `Upload by ${window.location.href}`,
|
message: `Upload by ${window.location.href}`,
|
||||||
},
|
},
|
||||||
@ -297,6 +241,40 @@ async function txCOSFileUpload(file) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
function getDir() {
|
||||||
fileUpload,
|
const date = new Date();
|
||||||
};
|
const year = date.getFullYear();
|
||||||
|
const month = (date.getMonth() + 1).toString().padStart(2, "0");
|
||||||
|
const day = date.getDate().toString().padStart(2, "0");
|
||||||
|
return `${year}/${month}/${day}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDateFilename(filename) {
|
||||||
|
const currentTimestamp = new Date().getTime();
|
||||||
|
const fileSuffix = filename.split(".")[1];
|
||||||
|
return `${currentTimestamp}-${uuidv4()}.${fileSuffix}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fileUpload(content, file) {
|
||||||
|
const imgHost = localStorage.getItem("imgHost");
|
||||||
|
!imgHost && localStorage.setItem("imgHost", "default");
|
||||||
|
switch (imgHost) {
|
||||||
|
case "aliOSS":
|
||||||
|
return aliOSSFileUpload(content, file.name);
|
||||||
|
case "txCOS":
|
||||||
|
return txCOSFileUpload(file);
|
||||||
|
case "qiniu":
|
||||||
|
return qiniuUpload(file);
|
||||||
|
case "gitee":
|
||||||
|
return giteeUpload(content, file.name);
|
||||||
|
case "github":
|
||||||
|
return ghFileUpload(content, file.name);
|
||||||
|
default:
|
||||||
|
// return file.size / 1024 < 1024
|
||||||
|
// ? giteeUpload(content, file.name)
|
||||||
|
// : ghFileUpload(content, file.name);
|
||||||
|
return ghFileUpload(content, file.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default fileUpload;
|
||||||
|
Loading…
Reference in New Issue
Block a user