mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-05-31 16:39:43 +08:00
✨ Feature(custom): support gitea for setting file sync
This commit is contained in:
@@ -14,7 +14,8 @@ interface SyncConfig {
|
||||
repo: string
|
||||
branch: string
|
||||
token: string
|
||||
proxy?: string,
|
||||
endpoint?: string
|
||||
proxy?: string
|
||||
interval?: number
|
||||
}
|
||||
|
||||
@@ -81,7 +82,7 @@ async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||
logger.error(error)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
} else if (type === 'github') {
|
||||
const octokit = getOctokit(syncConfig)
|
||||
try {
|
||||
const res = await octokit.rest.repos.createOrUpdateFileContents({
|
||||
@@ -97,6 +98,25 @@ async function uploadLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||
logger.error(error)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
const { endpoint = '' } = syncConfig
|
||||
const apiUrl = `${endpoint}/api/v1/repos/${username}/${repo}/contents/${fileName}`
|
||||
try {
|
||||
const headers = {
|
||||
Authorization: `token ${token}`
|
||||
}
|
||||
const res = await axios.post(apiUrl, {
|
||||
message: `upload ${fileName} from PicList`,
|
||||
content: fs.readFileSync(localFilePath, { encoding: 'base64' }),
|
||||
branch
|
||||
}, {
|
||||
headers
|
||||
})
|
||||
return res.status >= 200 && res.status < 300
|
||||
} catch (error: any) {
|
||||
logger.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,9 +152,8 @@ async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
} else {
|
||||
} else if (type === 'github') {
|
||||
const octokit = getOctokit(syncConfig)
|
||||
|
||||
const shaRes = await octokit.rest.repos.getContent({
|
||||
owner: username,
|
||||
repo,
|
||||
@@ -156,6 +175,29 @@ async function updateLocalToRemote (syncConfig: SyncConfig, fileName: string) {
|
||||
sha
|
||||
})
|
||||
return res.status === 200
|
||||
} else {
|
||||
const { endpoint = '' } = syncConfig
|
||||
const apiUrl = `${endpoint}/api/v1/repos/${username}/${repo}/contents/${fileName}`
|
||||
const headers = {
|
||||
Authorization: `token ${token}`
|
||||
}
|
||||
const shaRes = await axios.get(apiUrl, {
|
||||
headers
|
||||
})
|
||||
if (shaRes.status < 200 || shaRes.status > 300) {
|
||||
throw new Error('get sha failed')
|
||||
}
|
||||
const data = shaRes.data as any
|
||||
const sha = data.sha
|
||||
const res = await axios.put(apiUrl, {
|
||||
message: `update ${fileName} from PicList`,
|
||||
content: fs.readFileSync(localFilePath, { encoding: 'base64' }),
|
||||
branch,
|
||||
sha
|
||||
}, {
|
||||
headers
|
||||
})
|
||||
return res.status >= 200 && res.status < 300
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,20 +205,25 @@ async function downloadRemoteToLocal (syncConfig: SyncConfig, fileName: string)
|
||||
const localFilePath = path.join(STORE_PATH, fileName)
|
||||
const { username, repo, branch, token, proxy, type } = syncConfig
|
||||
if (type === 'gitee') {
|
||||
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
||||
const res = await axios.get(url, {
|
||||
params: {
|
||||
access_token: token,
|
||||
ref: branch
|
||||
try {
|
||||
const url = `https://gitee.com/api/v5/repos/${username}/${repo}/contents/${fileName}`
|
||||
const res = await axios.get(url, {
|
||||
params: {
|
||||
access_token: token,
|
||||
ref: branch
|
||||
}
|
||||
})
|
||||
if (res.status >= 200 && res.status < 300) {
|
||||
const content = res.data.content
|
||||
await fs.writeFile(localFilePath, Buffer.from(content, 'base64'))
|
||||
return true
|
||||
}
|
||||
})
|
||||
if (res.status >= 200 && res.status < 300) {
|
||||
const content = res.data.content
|
||||
await fs.writeFile(localFilePath, Buffer.from(content, 'base64'))
|
||||
return true
|
||||
return false
|
||||
} catch (error: any) {
|
||||
logger.error(error)
|
||||
return false
|
||||
}
|
||||
return false
|
||||
} else {
|
||||
} else if (type === 'github') {
|
||||
const octokit = getOctokit(syncConfig)
|
||||
try {
|
||||
const res = await octokit.rest.repos.getContent({
|
||||
@@ -209,6 +256,29 @@ async function downloadRemoteToLocal (syncConfig: SyncConfig, fileName: string)
|
||||
logger.error(error)
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
const { endpoint = '' } = syncConfig
|
||||
const apiUrl = `${endpoint}/api/v1/repos/${username}/${repo}/contents/${fileName}`
|
||||
try {
|
||||
const headers = {
|
||||
Authorization: `token ${token}`
|
||||
}
|
||||
const res = await axios.get(apiUrl, {
|
||||
headers,
|
||||
params: {
|
||||
ref: branch
|
||||
}
|
||||
})
|
||||
if (res.status >= 200 && res.status < 300) {
|
||||
const content = res.data.content
|
||||
await fs.writeFile(localFilePath, Buffer.from(content, 'base64'))
|
||||
return true
|
||||
}
|
||||
return false
|
||||
} catch (error: any) {
|
||||
logger.error(error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1129,39 +1129,49 @@
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
:label="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_USERNAME') : $T('SETTINGS_SYNC_CONFIG_GITEE_USERNAME')"
|
||||
v-if="sync.type === 'gitea'"
|
||||
:label="$T('SETTINGS_SYNC_CONFIG_GITEA_HOST')"
|
||||
>
|
||||
<el-input
|
||||
v-model.trim="sync.endpoint"
|
||||
type="input"
|
||||
:placeholder="$T('SETTINGS_SYNC_CONFIG_GITEA_HOST')"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
:label="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_USERNAME` as any)"
|
||||
>
|
||||
<el-input
|
||||
v-model.trim="sync.username"
|
||||
type="input"
|
||||
:placeholder="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER') : $T('SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER')"
|
||||
:placeholder="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_USERNAME_PLACEHOLDER` as any)"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
:label="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_REPO') : $T('SETTINGS_SYNC_CONFIG_GITEE_REPO')"
|
||||
:label="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_REPO` as any)"
|
||||
>
|
||||
<el-input
|
||||
v-model.trim="sync.repo"
|
||||
type="input"
|
||||
:placeholder="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER') : $T('SETTINGS_SYNC_CONFIG_GITEE_REPO_PLACEHOLDER')"
|
||||
:placeholder="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_REPO_PLACEHOLDER` as any)"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
:label="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_BRANCH') : $T('SETTINGS_SYNC_CONFIG_GITEE_BRANCH')"
|
||||
:label="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_BRANCH` as any)"
|
||||
>
|
||||
<el-input
|
||||
v-model.trim="sync.branch"
|
||||
type="input"
|
||||
:placeholder="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_BRANCH_PLACEHOLDER') : $T('SETTINGS_SYNC_CONFIG_GITEE_BRANCH_PLACEHOLDER')"
|
||||
:placeholder="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_BRANCH_PLACEHOLDER` as any)"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
:label="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_TOKEN') : $T('SETTINGS_SYNC_CONFIG_GITEE_TOKEN')"
|
||||
:label="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_TOKEN` as any)"
|
||||
>
|
||||
<el-input
|
||||
v-model.trim="sync.token"
|
||||
type="input"
|
||||
:placeholder="sync.type === 'github' ? $T('SETTINGS_SYNC_CONFIG_GITHUB_TOKEN_PLACEHOLDER') : $T('SETTINGS_SYNC_CONFIG_GITEE_TOKEN_PLACEHOLDER')"
|
||||
:placeholder="$T(`SETTINGS_SYNC_CONFIG_${sync.type.toUpperCase()}_TOKEN_PLACEHOLDER` as any)"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item
|
||||
@@ -1785,6 +1795,7 @@ const sync = ref({
|
||||
repo: '',
|
||||
branch: '',
|
||||
token: '',
|
||||
endpoint: '',
|
||||
proxy: '',
|
||||
interval: 60
|
||||
})
|
||||
@@ -1797,6 +1808,10 @@ const syncType = [
|
||||
{
|
||||
label: 'Gitee',
|
||||
value: 'gitee'
|
||||
},
|
||||
{
|
||||
label: 'Gitea',
|
||||
value: 'gitea'
|
||||
}
|
||||
]
|
||||
|
||||
@@ -1808,6 +1823,7 @@ async function cancelSyncSetting () {
|
||||
repo: '',
|
||||
branch: '',
|
||||
token: '',
|
||||
endpoint: '',
|
||||
proxy: '',
|
||||
interval: 60
|
||||
}
|
||||
@@ -1904,6 +1920,7 @@ async function initData () {
|
||||
repo: '',
|
||||
branch: '',
|
||||
token: '',
|
||||
endpoint: '',
|
||||
proxy: '',
|
||||
interval: 60
|
||||
}
|
||||
|
||||
9
src/universal/types/i18n.d.ts
vendored
9
src/universal/types/i18n.d.ts
vendored
@@ -245,23 +245,32 @@ interface ILocales {
|
||||
SETTINGS_SYNC_CONFIG_SELECT_CONFIG: string
|
||||
SETTINGS_SYNC_CONFIG_SELECT_GALLERY: string
|
||||
SETTINGS_SYNC_CONFIG_SELECT_BOTH: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_HOST: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_USERNAME: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_USERNAME: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_USERNAME: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_REPO: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_REPO: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_REPO: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_BRANCH: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_BRANCH: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_BRANCH: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_TOKEN: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_TOKEN: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_TOKEN: string
|
||||
SETTINGS_SYNC_CONFIG_PROXY: string
|
||||
SETTINGS_SYNC_CONFIG_INTERVAL: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_USERNAME_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_USERNAME_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_USERNAME_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_REPO_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_REPO_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_REPO_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_BRANCH_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_BRANCH_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_BRANCH_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITHUB_TOKEN_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEA_TOKEN_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_GITEE_TOKEN_PLACEHOLDER: string
|
||||
SETTINGS_SYNC_CONFIG_PROXY_PLACEHOLDER: string
|
||||
SETTINGS_UP_DOWN_DESC: string
|
||||
|
||||
Reference in New Issue
Block a user