-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy paths3.tsx
More file actions
286 lines (267 loc) · 9.85 KB
/
s3.tsx
File metadata and controls
286 lines (267 loc) · 9.85 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use client';
import { useState, useEffect } from 'react';
import { useTranslations } from 'next-intl';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
import { Eye, EyeOff, CheckCircle, XCircle, Loader2 } from 'lucide-react';
import { Switch } from '@/components/ui/switch';
import { toast } from '@/hooks/use-toast';
import useImageStore from '@/stores/imageHosting';
import { SyncStateEnum } from '@/lib/sync/github.types';
import { testS3Connection } from '@/lib/imageHosting/s3';
import { Store } from '@tauri-apps/plugin-store';
interface S3Config {
accessKeyId: string
secretAccessKey: string
region: string
bucket: string
endpoint?: string
customDomain?: string
pathPrefix?: string
forcePathStyle?: boolean
}
export function S3ImageHosting() {
const t = useTranslations();
const { setS3Config, s3State, setS3State, mainImageHosting, setMainImageHosting } = useImageStore();
const [config, setConfig] = useState<S3Config>({
accessKeyId: '',
secretAccessKey: '',
region: 'us-east-1',
bucket: '',
endpoint: '',
customDomain: '',
pathPrefix: '',
forcePathStyle: false
});
const [showSecretKey, setShowSecretKey] = useState(false);
// 初始化配置
useEffect(() => {
const initConfig = async () => {
const store = await Store.load('store.json');
const savedConfig = await store.get<S3Config>('s3Config');
if (savedConfig) {
setConfig(savedConfig);
// 如果配置完整,自动进行连接检测
if (savedConfig.accessKeyId && savedConfig.secretAccessKey && savedConfig.region && savedConfig.bucket) {
setS3State(SyncStateEnum.checking);
try {
const isConnected = await testS3Connection(savedConfig);
if (isConnected) {
setS3State(SyncStateEnum.success);
} else {
setS3State(SyncStateEnum.fail);
}
} catch (error) {
setS3State(SyncStateEnum.fail);
console.error('S3 connection test failed:', error);
}
}
}
};
initConfig();
}, [setS3Config]);
// 自动保存和测试配置
const handleConfigChange = async (newConfig: S3Config) => {
setConfig(newConfig);
// 自动保存配置
try {
await setS3Config(newConfig);
} catch (error) {
console.error('Failed to save S3 config:', error);
}
// 如果必填字段都已填写,自动测试连接
if (newConfig.accessKeyId && newConfig.secretAccessKey && newConfig.region && newConfig.bucket) {
setS3State(SyncStateEnum.checking);
try {
const isConnected = await testS3Connection(newConfig);
if (isConnected) {
setS3State(SyncStateEnum.success);
} else {
setS3State(SyncStateEnum.fail);
}
} catch (error) {
setS3State(SyncStateEnum.fail);
console.error('S3 connection test failed:', error);
}
} else {
setS3State(SyncStateEnum.fail);
}
};
// 设为主要图床
const handleSetAsPrimary = async () => {
if (s3State !== SyncStateEnum.success) {
toast({
title: t('settings.imageHosting.s3.error'),
description: t('settings.imageHosting.s3.testFirstDesc'),
variant: 'destructive',
});
return;
}
await setMainImageHosting('s3');
toast({
title: t('settings.imageHosting.s3.setPrimarySuccess'),
description: t('settings.imageHosting.s3.setPrimarySuccessDesc'),
});
};
const getStatusIcon = () => {
switch (s3State) {
case SyncStateEnum.success:
return <CheckCircle className="size-4 text-green-500" />;
case SyncStateEnum.checking:
return <Loader2 className="size-4 animate-spin text-blue-500" />;
case SyncStateEnum.fail:
default:
return <XCircle className="size-4 text-red-500" />;
}
};
const getStatusText = () => {
switch (s3State) {
case SyncStateEnum.success:
return t('settings.imageHosting.s3.connected');
case SyncStateEnum.checking:
return t('settings.imageHosting.s3.connecting');
case SyncStateEnum.fail:
default:
return t('settings.imageHosting.s3.disconnected');
}
};
return (
<Card>
<CardHeader>
<div className="flex items-center justify-between">
<div>
<CardTitle>{t('settings.imageHosting.s3.title')}</CardTitle>
<CardDescription>
{t('settings.imageHosting.s3.description')}
</CardDescription>
</div>
<Button
onClick={handleSetAsPrimary}
disabled={mainImageHosting === 's3' || s3State !== SyncStateEnum.success}
size="sm"
>
{mainImageHosting === 's3' ?
'当前主要图床' :
t('settings.imageHosting.s3.setAsPrimary')
}
</Button>
</div>
</CardHeader>
<CardContent className="space-y-4">
{/* 状态显示 */}
<div className="flex items-center justify-between p-3 bg-muted rounded-lg">
<span className="text-sm font-medium">{t('settings.imageHosting.s3.status')}</span>
<div className="flex items-center gap-2">
{getStatusIcon()}
<span className="text-sm">{getStatusText()}</span>
</div>
</div>
{/* 基本配置 */}
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="accessKeyId">{t('settings.imageHosting.s3.accessKeyId')}</Label>
<Input
id="accessKeyId"
type="text"
value={config.accessKeyId}
onChange={(e) => handleConfigChange({ ...config, accessKeyId: e.target.value })}
placeholder={t('settings.imageHosting.s3.accessKeyIdPlaceholder')}
/>
</div>
<div className="space-y-2">
<Label htmlFor="secretAccessKey">{t('settings.imageHosting.s3.secretAccessKey')}</Label>
<div className="relative">
<Input
id="secretAccessKey"
type={showSecretKey ? "text" : "password"}
value={config.secretAccessKey}
onChange={(e) => handleConfigChange({ ...config, secretAccessKey: e.target.value })}
placeholder={t('settings.imageHosting.s3.secretAccessKeyPlaceholder')}
className="pr-10"
/>
<Button
type="button"
variant="ghost"
size="sm"
className="absolute right-0 top-0 h-full px-3 py-2 hover:bg-transparent"
onClick={() => setShowSecretKey(!showSecretKey)}
>
{showSecretKey ? <EyeOff className="size-4" /> : <Eye className="size-4" />}
</Button>
</div>
</div>
<div className="space-y-2">
<Label htmlFor="region">{t('settings.imageHosting.s3.region')}</Label>
<Input
id="region"
type="text"
value={config.region}
onChange={(e) => handleConfigChange({ ...config, region: e.target.value })}
placeholder="us-east-1"
/>
</div>
<div className="space-y-2">
<Label htmlFor="bucket">{t('settings.imageHosting.s3.bucket')}</Label>
<Input
id="bucket"
type="text"
value={config.bucket}
onChange={(e) => handleConfigChange({ ...config, bucket: e.target.value })}
placeholder={t('settings.imageHosting.s3.bucketPlaceholder')}
/>
</div>
</div>
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="endpoint">{t('settings.imageHosting.s3.endpoint')}</Label>
<Input
id="endpoint"
type="text"
value={config.endpoint || ''}
onChange={(e) => handleConfigChange({ ...config, endpoint: e.target.value })}
placeholder="https://s3.amazonaws.com"
/>
</div>
<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label htmlFor="forcePathStyle">{t('settings.imageHosting.s3.forcePathStyle')}</Label>
<p className="text-xs text-muted-foreground">
{t('settings.imageHosting.s3.forcePathStyleDesc')}
</p>
</div>
<Switch
id="forcePathStyle"
checked={config.forcePathStyle || false}
onCheckedChange={(checked) => handleConfigChange({ ...config, forcePathStyle: checked })}
/>
</div>
<div className="space-y-2">
<Label htmlFor="customDomain">{t('settings.imageHosting.s3.customDomain')}</Label>
<Input
id="customDomain"
type="text"
value={config.customDomain || ''}
onChange={(e) => handleConfigChange({ ...config, customDomain: e.target.value })}
placeholder="https://cdn.example.com"
/>
</div>
<div className="space-y-2">
<Label htmlFor="pathPrefix">{t('settings.imageHosting.s3.pathPrefix')}</Label>
<Input
id="pathPrefix"
type="text"
value={config.pathPrefix || ''}
onChange={(e) => handleConfigChange({ ...config, pathPrefix: e.target.value })}
placeholder="images/"
/>
<p className="text-xs text-muted-foreground">
{t('settings.imageHosting.s3.pathPrefixDesc')}
</p>
</div>
</div>
</CardContent>
</Card>
);
}