mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2026-04-03 05:53:25 +08:00
Bump to ES6
This commit is contained in:
@@ -1,75 +1,77 @@
|
||||
const { HTTP } = require('./open-api');
|
||||
import { HTTP } from './open-api';
|
||||
|
||||
/**
|
||||
* Gist backup
|
||||
*/
|
||||
function Gist({ token, key }) {
|
||||
const http = HTTP({
|
||||
baseURL: 'https://api.github.com',
|
||||
headers: {
|
||||
Authorization: `token ${token}`,
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36'
|
||||
},
|
||||
events: {
|
||||
onResponse: (resp) => {
|
||||
if (/^[45]/.test(String(resp.statusCode))) {
|
||||
return Promise.reject(`ERROR: ${JSON.parse(resp.body).message}`);
|
||||
} else {
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
export default function Gist({ token, key }) {
|
||||
const http = HTTP({
|
||||
baseURL: 'https://api.github.com',
|
||||
headers: {
|
||||
Authorization: `token ${token}`,
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36',
|
||||
},
|
||||
events: {
|
||||
onResponse: (resp) => {
|
||||
if (/^[45]/.test(String(resp.statusCode))) {
|
||||
return Promise.reject(
|
||||
`ERROR: ${JSON.parse(resp.body).message}`,
|
||||
);
|
||||
} else {
|
||||
return resp;
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
async function locate() {
|
||||
return http.get('/gists').then((response) => {
|
||||
const gists = JSON.parse(response.body);
|
||||
for (let g of gists) {
|
||||
if (g.description === key) {
|
||||
return g.id;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
}
|
||||
async function locate() {
|
||||
return http.get('/gists').then((response) => {
|
||||
const gists = JSON.parse(response.body);
|
||||
for (let g of gists) {
|
||||
if (g.description === key) {
|
||||
return g.id;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
}
|
||||
|
||||
this.upload = async function(files) {
|
||||
const id = await locate();
|
||||
this.upload = async function (files) {
|
||||
const id = await locate();
|
||||
|
||||
if (id === -1) {
|
||||
// create a new gist for backup
|
||||
return http.post({
|
||||
url: '/gists',
|
||||
body: JSON.stringify({
|
||||
description: key,
|
||||
public: false,
|
||||
files
|
||||
})
|
||||
});
|
||||
} else {
|
||||
// update an existing gist
|
||||
return http.patch({
|
||||
url: `/gists/${id}`,
|
||||
body: JSON.stringify({ files })
|
||||
});
|
||||
}
|
||||
};
|
||||
if (id === -1) {
|
||||
// create a new gist for backup
|
||||
return http.post({
|
||||
url: '/gists',
|
||||
body: JSON.stringify({
|
||||
description: key,
|
||||
public: false,
|
||||
files,
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
// update an existing gist
|
||||
return http.patch({
|
||||
url: `/gists/${id}`,
|
||||
body: JSON.stringify({ files }),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
this.download = async function(filename) {
|
||||
const id = await locate();
|
||||
if (id === -1) {
|
||||
return Promise.reject('未找到Gist备份!');
|
||||
} else {
|
||||
try {
|
||||
const { files } = await http.get(`/gists/${id}`).then((resp) => JSON.parse(resp.body));
|
||||
const url = files[filename].raw_url;
|
||||
return await http.get(url).then((resp) => resp.body);
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
};
|
||||
this.download = async function (filename) {
|
||||
const id = await locate();
|
||||
if (id === -1) {
|
||||
return Promise.reject('未找到Gist备份!');
|
||||
} else {
|
||||
try {
|
||||
const { files } = await http
|
||||
.get(`/gists/${id}`)
|
||||
.then((resp) => JSON.parse(resp.body));
|
||||
const url = files[filename].raw_url;
|
||||
return await http.get(url).then((resp) => resp.body);
|
||||
} catch (err) {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = Gist;
|
||||
|
||||
Reference in New Issue
Block a user