Skip to content

Commit f931cf1

Browse files
committed
refactor: 拆分图床配置,可配置独立的 Github 图床 (#306)
* refactor: 增加图床配置,Github 图床配置移至图床配置 * feat: Github 图床可配置独立的 token * feat: 上传图片使用独立 Github 图床
1 parent ede4e11 commit f931cf1

File tree

27 files changed

+611
-272
lines changed

27 files changed

+611
-272
lines changed

messages/en.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
},
110110
"sync": {
111111
"title": "Sync",
112-
"desc": "Here, you can add and manage repositories, helping you sync your notes and images.",
112+
"desc": "Here, you can configure the synchronization repository, which can help you synchronize records, markdown files, system configurations and other information.",
113113
"repoStatus": "Repository Status",
114114
"syncRepo": "Sync Repository",
115115
"syncRepoDesc": "Sync markdown files in writing",
@@ -142,6 +142,12 @@
142142
"30m": "30 minutes"
143143
}
144144
},
145+
"imageHosting": {
146+
"title": "Image Hosting",
147+
"desc": "Here, you can configure image hosting, uploading images to the image hosting service.",
148+
"isPrimaryBackup": "Current {type} primary image hosting method",
149+
"setPrimaryBackup": "Set as Primary Image Hosting"
150+
},
145151
"backupSync": {
146152
"title": "Backup Data",
147153
"desc": "Here, you can use other methods to backup your data, you can regularly backup to ensure data security.",

messages/ja.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
},
108108
"sync": {
109109
"title": "同期",
110-
"desc": "ここで同期リポジトリを設定し、執筆中のファイルをリポジトリと同期できます。画像ホスティングもサポートしています",
110+
"desc": "ここでは、同期リポジトリを設定することができます。ログ、markdownファイル、システム設定などの情報を同期するのに役立ちます",
111111
"repoStatus": "リポジトリの状態",
112112
"syncRepo": "同期リポジトリ",
113113
"syncRepoDesc": "執筆中のMarkdownファイルを同期",
@@ -140,8 +140,14 @@
140140
"30m": "30 分"
141141
}
142142
},
143+
"imageHosting": {
144+
"title": "画像ホスティング",
145+
"desc": "ここでは、画像ホスティングを設定できます。",
146+
"isPrimaryBackup": "現在の {type} 主要バックアップ方法",
147+
"setPrimaryBackup": "主要バックアップ方法として設定"
148+
},
143149
"backupSync": {
144-
"title": "データバックアップ",
150+
"title": "備用方案",
145151
"desc": "ここでは、他の方法でデータをバックアップすることができます。定期的にバックアップを取ることで、データの安全を確保できます。",
146152
"webdav": {
147153
"connectionState": {

messages/zh.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@
107107
},
108108
"sync": {
109109
"title": "同步配置",
110-
"desc": "在这里,你可以配置同步仓库,将写作中的文件同步到仓库,并支持图床",
110+
"desc": "在这里,你可以配置同步仓库,它可以帮助你同步记录、markdown 文件、系统配置等信息",
111111
"repoStatus": "仓库状态",
112112
"syncRepo": "同步仓库",
113-
"syncRepoDesc": "同步写作中的 markdown 文件",
113+
"syncRepoDesc": "同步写作中的 markdown 文件",
114114
"imageRepo": "图床仓库",
115115
"imageRepoDesc": "同步你的图片到图床仓库。",
116116
"private": "私有",
@@ -140,8 +140,14 @@
140140
"30m": "30 分钟"
141141
}
142142
},
143+
"imageHosting": {
144+
"title": "图床",
145+
"desc": "在这里,你可以配置图床,将图片上传到图床。",
146+
"isPrimaryBackup": "当前 {type} 为主要图床",
147+
"setPrimaryBackup": "设为主要图床"
148+
},
143149
"backupSync": {
144-
"title": "数据备份",
150+
"title": "备用方案",
145151
"desc": "在这里,你可以使用其他方案来备份你的数据,你可以定期进行备份,以确保数据的安全。",
146152
"webdav": {
147153
"connectionState": {

src/app/core/article/md-editor.tsx

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import CustomToolbar from './custom-toolbar'
88
import './style.scss'
99
import { useTheme } from 'next-themes'
1010
import { toast } from '@/hooks/use-toast'
11-
import { fileToBase64, uploadFile } from '@/lib/github'
12-
import { RepoNames } from '@/lib/github.types'
1311
import { Store } from '@tauri-apps/plugin-store'
1412
import { useTranslations } from 'next-intl'
1513
import { useI18n } from '@/hooks/useI18n'
1614
import emitter from '@/lib/emitter'
17-
import dayjs from 'dayjs'
1815
import { appDataDir } from '@tauri-apps/api/path'
1916
import { v4 as uuid } from 'uuid'
2017
import { convertImage } from '@/lib/utils'
@@ -25,6 +22,7 @@ import { isMobileDevice } from '@/lib/check'
2522
import { getWorkspacePath } from '@/lib/workspace'
2623
import { convertFileSrc } from "@tauri-apps/api/core";
2724
import useSettingStore from '@/stores/setting'
25+
import { uploadImage } from '@/lib/imageHosting'
2826

2927
export function MdEditor() {
3028
const [editor, setEditor] = useState<Vditor>();
@@ -173,7 +171,7 @@ export function MdEditor() {
173171
upload: {
174172
async handler(files: File[]) {
175173
const store = await Store.load('store.json');
176-
const accessToken = await store.get('accessToken')
174+
const accessToken = await store.get('githubImageAccessToken')
177175
const useImageRepo = await store.get('useImageRepo')
178176
if (accessToken && useImageRepo) {
179177
const filesUrls = await uploadImages(files)
@@ -257,23 +255,7 @@ export function MdEditor() {
257255
description: file.name,
258256
duration: 600000,
259257
})
260-
const path = dayjs().format('YYYY-MM')
261-
const fileBase64 = await fileToBase64(file)
262-
const ext = file.name.split('.')[file.name.split('.').length - 1]
263-
await uploadFile({
264-
ext,
265-
file: fileBase64,
266-
repo: RepoNames.image,
267-
path
268-
}).then(async res => {
269-
const store = await Store.load('store.json');
270-
const jsdelivr = await store.get('jsdelivr')
271-
let url = res?.data.content.download_url
272-
if (jsdelivr) {
273-
const githubUsername = await store.get('githubUsername')
274-
await fetch(`https://purge.jsdelivr.net/gh/${githubUsername}/${RepoNames.image}@main/${path}/${res?.data.content.name}`)
275-
url = `https://cdn.jsdelivr.net/gh/${githubUsername}/${RepoNames.image}@main/${path}/${res?.data.content.name}`
276-
}
258+
await uploadImage(file).then(async url => {
277259
resolve(url)
278260
}).catch(err => {
279261
reject(err)

src/app/core/image/folder-card.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
CardContent,
44
} from "@/components/ui/card"
55
import { GithubFile } from "@/lib/github"
6-
import useImageStore from "@/stores/image"
6+
import useImageStore from "@/stores/imageHosting"
77
import { Folder } from 'lucide-react'
88

99
export function FolderCard({file}: {file: GithubFile}) {

src/app/core/image/image-card.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { GithubFile } from "@/lib/github"
77
import { convertBytesToSize } from '@/lib/utils'
88
import { deleteFile } from "@/lib/github"
99
import { toast } from "@/hooks/use-toast"
10-
import useImageStore from "@/stores/image"
10+
import useImageStore from "@/stores/imageHosting"
1111
import { RepoNames } from "@/lib/github.types"
1212
import { PhotoProvider, PhotoView } from 'react-photo-view';
1313
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger } from "@/components/ui/context-menu"

src/app/core/image/image-header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {TooltipButton} from "@/components/tooltip-button"
33
import {TooltipProvider} from "@/components/ui/tooltip"
44
import {RefreshCcw} from "lucide-react"
55
import * as React from "react"
6-
import useImageStore from "@/stores/image"
6+
import useImageStore from "@/stores/imageHosting"
77
import {Separator} from "@/components/ui/separator"
88
import {convertBytesToSize} from "@/lib/utils"
99
import {open} from '@tauri-apps/plugin-shell';

src/app/core/image/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { fileToBase64, GithubFile, uploadFile } from '@/lib/github'
33
import { useEffect, useState } from 'react'
44
import { ImageCard } from './image-card'
5-
import useImageStore from '@/stores/image'
5+
import useImageStore from '@/stores/imageHosting'
66
import useMarkStore from '@/stores/mark'
77
import { ImageHeader } from './image-header'
88
import useSettingStore from '@/stores/setting'

src/app/core/layout.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@ import zh from "dayjs/locale/zh-cn";
1111
import en from "dayjs/locale/en";
1212
import { useI18n } from "@/hooks/useI18n"
1313
import useVectorStore from "@/stores/vector"
14+
import useImageStore from "@/stores/imageHosting"
1415

1516
export default function RootLayout({
1617
children,
1718
}: Readonly<{
1819
children: React.ReactNode;
1920
}>) {
2021
const { initSettingData } = useSettingStore()
22+
const { initMainHosting } = useImageStore()
2123
const { currentLocale } = useI18n()
2224
useEffect(() => {
2325
initSettingData()
26+
initMainHosting()
2427
initAllDatabases()
2528
}, [])
2629

src/app/core/record/mark/control-image.tsx

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import useMarkStore from "@/stores/mark"
77
import useTagStore from "@/stores/tag"
88
import { BaseDirectory, copyFile, exists, mkdir, readFile } from "@tauri-apps/plugin-fs"
99
import { ImagePlus } from "lucide-react"
10-
import { uploadFile, uint8ArrayToBase64 } from "@/lib/github"
1110
import useSettingStore from "@/stores/setting"
1211
import { v4 as uuid } from 'uuid'
13-
import { RepoNames } from "@/lib/github.types"
1412
import { open } from '@tauri-apps/plugin-dialog';
15-
import dayjs from "dayjs"
13+
import { uploadImage } from "@/lib/imageHosting"
1614

1715
export function ControlImage() {
1816
const t = useTranslations();
@@ -31,11 +29,11 @@ export function ControlImage() {
3129
});
3230
if (!filePaths) return
3331
filePaths.forEach(async (path) => {
34-
await uploadImage(path)
32+
await upload(path)
3533
})
3634
}
3735

38-
async function uploadImage(path: string) {
36+
async function upload(path: string) {
3937
const queueId = uuid()
4038
addQueue({ queueId, progress: t('record.mark.progress.cacheImage'), type: 'image', startTime: Date.now() })
4139
const ext = path.substring(path.lastIndexOf('.') + 1)
@@ -44,7 +42,7 @@ export function ControlImage() {
4442
await mkdir('image', { baseDir: BaseDirectory.AppData})
4543
}
4644
await copyFile(path, `image/${queueId}.${ext}`, { toPathBaseDir: BaseDirectory.AppData})
47-
const file = await readFile(path)
45+
const fileData = await readFile(path)
4846
const filename = `${queueId}.${ext}`
4947
setQueue(queueId, { progress: t('record.mark.progress.ocr') });
5048
const content = await ocr(`image/${filename}`)
@@ -64,20 +62,10 @@ export function ControlImage() {
6462
}
6563
if (githubUsername) {
6664
setQueue(queueId, { progress: t('record.mark.progress.uploadImage') });
67-
const path = dayjs().format('YYYY-MM')
68-
const res = await uploadFile({
69-
ext,
70-
file: uint8ArrayToBase64(file),
71-
filename,
72-
repo: RepoNames.image,
73-
path
74-
})
75-
if (res) {
76-
setQueue(queueId, { progress: t('record.mark.progress.jsdelivrCache') });
77-
await fetch(`https://purge.jsdelivr.net/gh/${githubUsername}/${RepoNames.image}@main/${path}/${res.data.content.name}`)
78-
mark.url = `https://cdn.jsdelivr.net/gh/${githubUsername}/${RepoNames.image}@main/${path}/${res.data.content.name}`
79-
} else {
80-
mark.url = filename
65+
const file = new File([new Blob([fileData])], filename, { type: `image/${ext}` })
66+
const url = await uploadImage(file)
67+
if (url) {
68+
mark.url = url
8169
}
8270
}
8371
removeQueue(queueId)

0 commit comments

Comments
 (0)