-
Notifications
You must be signed in to change notification settings - Fork 818
Expand file tree
/
Copy pathimageHosting.ts
More file actions
122 lines (108 loc) · 3.27 KB
/
imageHosting.ts
File metadata and controls
122 lines (108 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { GithubFile } from '@/lib/sync/github';
import { getImageFiles } from '@/lib/imageHosting/github';
import { GithubRepoInfo, OctokitResponse, SyncStateEnum, UserInfo } from '@/lib/sync/github.types';
import { Store } from '@tauri-apps/plugin-store';
import { create } from 'zustand'
interface S3Config {
accessKeyId: string
secretAccessKey: string
region: string
bucket: string
endpoint?: string
customDomain?: string
pathPrefix?: string
forcePathStyle?: boolean
}
interface MarkState {
initMainHosting: () => Promise<void>
path: string
setPath: (path: string) => void
images: GithubFile[]
pushImage: (image: GithubFile) => void
deleteImage: (name: string) => void
getImages: () => Promise<void>
// 主要图床
mainImageHosting: string
setMainImageHosting: (mainImageHosting: string) => Promise<void>
// 图床 Github 仓库
imageRepoUserInfo?: OctokitResponse<UserInfo>
setImageRepoUserInfo: (imageRepoUserInfo?: OctokitResponse<UserInfo>) => Promise<void>
imageRepoState: SyncStateEnum
setImageRepoState: (imageRepoState: SyncStateEnum) => void
imageRepoInfo?: GithubRepoInfo
setImageRepoInfo: (imageRepoInfo?: GithubRepoInfo) => void
// S3 配置
s3Config?: S3Config
setS3Config: (config: S3Config) => Promise<void>
s3State: SyncStateEnum
setS3State: (state: SyncStateEnum) => void
}
const useImageStore = create<MarkState>((set, get) => ({
initMainHosting: async () => {
const store = await Store.load('store.json');
const mainImageHosting = await store.get<string>('mainImageHosting')
if (mainImageHosting) {
set({ mainImageHosting })
}
// 初始化 S3 配置
const s3Config = await store.get<S3Config>('s3Config');
if (s3Config) {
set({ s3Config })
}
},
path: '',
setPath: (path) => set({ path }),
images: [],
pushImage: (image) => {
set(state => ({
images: [image, ...state.images]
}))
},
deleteImage: (name) => {
set(state => ({
images: state.images.filter(item => item.name !== name)
}))
},
async getImages() {
set({ images: [] })
const images = await getImageFiles({ path: get().path })
set({ images: images || [] })
},
// 主要图床
mainImageHosting: 'github',
setMainImageHosting: async (mainImageHosting) => {
set({ mainImageHosting })
const store = await Store.load('store.json');
await store.set('mainImageHosting', mainImageHosting)
await store.save()
},
imageRepoUserInfo: undefined,
setImageRepoUserInfo: async (imageRepoUserInfo) => {
set({ imageRepoUserInfo })
if (!imageRepoUserInfo) return
const store = await Store.load('store.json');
await store.set('githubImageUsername', imageRepoUserInfo?.data?.login)
await store.save()
},
imageRepoState: SyncStateEnum.fail,
setImageRepoState: (imageRepoState) => {
set({ imageRepoState })
},
imageRepoInfo: undefined,
setImageRepoInfo: (imageRepoInfo) => {
set({ imageRepoInfo })
},
// S3 配置
s3Config: undefined,
setS3Config: async (config) => {
set({ s3Config: config })
const store = await Store.load('store.json');
await store.set('s3Config', config)
await store.save()
},
s3State: SyncStateEnum.fail,
setS3State: (s3State) => {
set({ s3State })
},
}))
export default useImageStore