@shellui/core 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/app.tsx +31 -3
- package/src/components/ui/dialog.tsx +26 -6
- package/src/components/ui/sonner.tsx +9 -5
- package/src/features/admin/AdminView.tsx +5 -49
- package/src/features/admin/config.ts +15 -0
- package/src/features/admin/index.ts +4 -0
- package/src/features/admin/routes.tsx +14 -0
- package/src/features/admin/utils.ts +79 -0
- package/src/features/auth/AuthProvider.tsx +68 -13
- package/src/features/auth/backends/shellui.ts +8 -1
- package/src/features/auth/components/AccessPendingView.tsx +112 -0
- package/src/features/auth/components/LoginButton.tsx +5 -10
- package/src/features/auth/components/LoginView.tsx +20 -2
- package/src/features/auth/components/OAuthCallbackView.tsx +211 -50
- package/src/features/auth/hooks/useAuth.tsx +8 -1
- package/src/features/auth/utils/authRequestError.spec.ts +52 -0
- package/src/features/auth/utils/authRequestError.ts +45 -0
- package/src/features/auth/utils/buildAuthUrlWithNext.spec.ts +18 -0
- package/src/features/auth/utils/buildAuthUrlWithNext.ts +12 -0
- package/src/features/auth/utils/index.ts +8 -0
- package/src/features/auth/utils/isTokenAutoRefreshDisabled.spec.ts +71 -0
- package/src/features/auth/utils/isTokenAutoRefreshDisabled.ts +18 -0
- package/src/features/config/ConfigProvider.ts +1 -1
- package/src/features/config/types.ts +46 -5
- package/src/features/layouts/AppLayout.tsx +3 -3
- package/src/features/layouts/OverlayShell.tsx +17 -28
- package/src/features/layouts/resolveSdkNavigatePath.spec.ts +55 -0
- package/src/features/layouts/resolveSdkNavigatePath.ts +68 -0
- package/src/features/settings/SettingsIcons.tsx +35 -0
- package/src/features/settings/SettingsProvider.tsx +79 -10
- package/src/features/settings/SettingsRoutes.tsx +14 -6
- package/src/features/settings/SettingsView.tsx +11 -5
- package/src/features/settings/components/Advanced.tsx +117 -44
- package/src/features/settings/components/Develop.tsx +38 -0
- package/src/features/settings/components/ServiceWorker.tsx +52 -25
- package/src/features/settings/components/Storage.tsx +138 -0
- package/src/features/settings/components/UpdateApp.tsx +1 -1
- package/src/features/settings/components/develop/StoragePickerTestButtons.tsx +148 -0
- package/src/features/settings/components/develop/UploadToastTestButton.tsx +29 -0
- package/src/features/settings/utils/buildSettingsForPropagation.spec.ts +55 -1
- package/src/features/settings/utils/buildSettingsForPropagation.ts +34 -0
- package/src/features/settings/utils/mergePreferencesIntoSettings.spec.ts +1 -1
- package/src/features/settings/utils/toSettingsUser.spec.ts +4 -0
- package/src/features/settings/utils/toSettingsUser.ts +2 -0
- package/src/features/sonner/SonnerContext.tsx +44 -60
- package/src/features/storage/StorageBridge.tsx +50 -0
- package/src/features/storage/StoragePickerContext.tsx +214 -0
- package/src/features/storage/api.spec.ts +303 -0
- package/src/features/storage/api.ts +414 -0
- package/src/features/storage/handleRequest.spec.ts +88 -0
- package/src/features/storage/handleRequest.ts +53 -0
- package/src/features/storage/quota.spec.ts +151 -0
- package/src/features/storage/quota.ts +104 -0
- package/src/features/storage/uploads/UploadToastCard.tsx +442 -0
- package/src/features/storage/uploads/UploadToaster.tsx +41 -0
- package/src/features/storage/uploads/handleTrackedUpload.spec.ts +170 -0
- package/src/features/storage/uploads/handleTrackedUpload.ts +97 -0
- package/src/features/storage/uploads/types.ts +24 -0
- package/src/features/storage/uploads/uploadQueue.spec.ts +118 -0
- package/src/features/storage/uploads/uploadQueue.ts +349 -0
- package/src/features/storage/uploads/useUploadQueue.ts +30 -0
- package/src/i18n/translations/en/common.json +33 -0
- package/src/i18n/translations/en/settings.json +56 -4
- package/src/i18n/translations/fr/common.json +33 -0
- package/src/i18n/translations/fr/settings.json +56 -4
- package/src/index.css +2 -1
- package/src/index.ts +2 -0
- package/src/lib/z-index.ts +9 -3
- package/src/routes/components/NavigationItemRoute.tsx +2 -1
- package/src/routes/routes.tsx +2 -13
- package/src/service-worker/register.ts +217 -101
- package/src/service-worker/sw-dev.ts +5 -1
- package/src/service-worker/sw.ts +5 -1
- package/src/shellui-target.d.ts +2 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import type { StorageRequestInput, StorageRequestPayload } from '@shellui/sdk';
|
|
2
|
+
|
|
3
|
+
export const FOLDER_PLACEHOLDER = '.emptyFolderPlaceholder';
|
|
4
|
+
|
|
5
|
+
export class StorageHttpError extends Error {
|
|
6
|
+
status: number;
|
|
7
|
+
code?: string;
|
|
8
|
+
|
|
9
|
+
constructor(message: string, status: number, code?: string) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = 'StorageHttpError';
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function normalizeStoragePath(path: string | undefined | null): string {
|
|
18
|
+
if (!path) return '';
|
|
19
|
+
return path.replace(/^\/+|\/+$/g, '');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function encodeObjectPath(bucket: string, path: string): string {
|
|
23
|
+
const trimmed = normalizeStoragePath(path);
|
|
24
|
+
const segments = trimmed ? trimmed.split('/').map(encodeURIComponent) : [];
|
|
25
|
+
return [encodeURIComponent(bucket), ...segments].join('/');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function objectUrl(storageBaseUrl: string, bucket: string, path: string): string {
|
|
29
|
+
return `${storageBaseUrl}/storage/v1/object/${encodeObjectPath(bucket, path)}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function parseError(response: Response): Promise<StorageHttpError> {
|
|
33
|
+
let message = response.statusText || 'Request failed';
|
|
34
|
+
let code: string | undefined;
|
|
35
|
+
try {
|
|
36
|
+
const body = (await response.json()) as { message?: string; error?: string; detail?: string };
|
|
37
|
+
message = body.message || body.error || body.detail || message;
|
|
38
|
+
code = body.error;
|
|
39
|
+
} catch {
|
|
40
|
+
/* ignore */
|
|
41
|
+
}
|
|
42
|
+
return new StorageHttpError(message, response.status, code);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function request<T>(
|
|
46
|
+
storageBaseUrl: string,
|
|
47
|
+
path: string,
|
|
48
|
+
accessToken: string,
|
|
49
|
+
init: RequestInit = {},
|
|
50
|
+
): Promise<T> {
|
|
51
|
+
const headers = new Headers(init.headers);
|
|
52
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
53
|
+
if (init.body && !(init.body instanceof Blob) && !(init.body instanceof FormData)) {
|
|
54
|
+
if (!headers.has('Content-Type')) headers.set('Content-Type', 'application/json');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const response = await fetch(`${storageBaseUrl}${path}`, { ...init, headers });
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
throw await parseError(response);
|
|
60
|
+
}
|
|
61
|
+
if (response.status === 204) return undefined as T;
|
|
62
|
+
const contentType = response.headers.get('Content-Type') || '';
|
|
63
|
+
if (contentType.includes('application/json')) {
|
|
64
|
+
return (await response.json()) as T;
|
|
65
|
+
}
|
|
66
|
+
return undefined as T;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function listBuckets(storageBaseUrl: string, accessToken: string): Promise<unknown> {
|
|
70
|
+
return request(storageBaseUrl, '/storage/v1/bucket', accessToken);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function listObjects(
|
|
74
|
+
storageBaseUrl: string,
|
|
75
|
+
accessToken: string,
|
|
76
|
+
bucket: string,
|
|
77
|
+
prefix = '',
|
|
78
|
+
options: {
|
|
79
|
+
limit?: number;
|
|
80
|
+
offset?: number;
|
|
81
|
+
sortBy?: { column: string; order: 'asc' | 'desc' };
|
|
82
|
+
} = {},
|
|
83
|
+
): Promise<unknown> {
|
|
84
|
+
const entries = await request<Array<{ name?: string }>>(
|
|
85
|
+
storageBaseUrl,
|
|
86
|
+
`/storage/v1/object/list/${encodeURIComponent(bucket)}`,
|
|
87
|
+
accessToken,
|
|
88
|
+
{
|
|
89
|
+
method: 'POST',
|
|
90
|
+
body: JSON.stringify({
|
|
91
|
+
prefix: normalizeStoragePath(prefix),
|
|
92
|
+
limit: options.limit ?? 100,
|
|
93
|
+
offset: options.offset ?? 0,
|
|
94
|
+
sortBy: options.sortBy ?? { column: 'name', order: 'asc' },
|
|
95
|
+
}),
|
|
96
|
+
},
|
|
97
|
+
);
|
|
98
|
+
return entries.filter((item) => item.name !== FOLDER_PLACEHOLDER);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function isFolderPlaceholderPath(path: string | undefined | null): boolean {
|
|
102
|
+
const normalized = normalizeStoragePath(path);
|
|
103
|
+
return normalized === FOLDER_PLACEHOLDER || normalized.endsWith(`/${FOLDER_PLACEHOLDER}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type UploadProgressHandler = (loaded: number, total: number) => void;
|
|
107
|
+
|
|
108
|
+
export type UploadObjectOptions = {
|
|
109
|
+
upsert?: boolean;
|
|
110
|
+
contentType?: string;
|
|
111
|
+
signal?: AbortSignal;
|
|
112
|
+
onProgress?: UploadProgressHandler;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
function parseXhrError(xhr: XMLHttpRequest): StorageHttpError {
|
|
116
|
+
let message = xhr.statusText || 'Request failed';
|
|
117
|
+
let code: string | undefined;
|
|
118
|
+
try {
|
|
119
|
+
const body = JSON.parse(xhr.responseText) as {
|
|
120
|
+
message?: string;
|
|
121
|
+
error?: string;
|
|
122
|
+
detail?: string;
|
|
123
|
+
};
|
|
124
|
+
message = body.message || body.error || body.detail || message;
|
|
125
|
+
code = typeof body.error === 'string' ? body.error : undefined;
|
|
126
|
+
} catch {
|
|
127
|
+
/* ignore */
|
|
128
|
+
}
|
|
129
|
+
return new StorageHttpError(message, xhr.status, code);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Upload with progress events and abort support (XMLHttpRequest). */
|
|
133
|
+
export function uploadObjectWithProgress(
|
|
134
|
+
storageBaseUrl: string,
|
|
135
|
+
accessToken: string,
|
|
136
|
+
bucket: string,
|
|
137
|
+
path: string,
|
|
138
|
+
file: Blob,
|
|
139
|
+
options: UploadObjectOptions = {},
|
|
140
|
+
): Promise<{ path: string }> {
|
|
141
|
+
const normalized = normalizeStoragePath(path);
|
|
142
|
+
const contentType = options.contentType || file.type || 'application/octet-stream';
|
|
143
|
+
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
const xhr = new XMLHttpRequest();
|
|
146
|
+
xhr.open('POST', objectUrl(storageBaseUrl, bucket, normalized));
|
|
147
|
+
xhr.setRequestHeader('Authorization', `Bearer ${accessToken}`);
|
|
148
|
+
xhr.setRequestHeader('Content-Type', contentType);
|
|
149
|
+
if (options.upsert) xhr.setRequestHeader('x-upsert', 'true');
|
|
150
|
+
|
|
151
|
+
xhr.upload.onprogress = (event) => {
|
|
152
|
+
if (!options.onProgress) return;
|
|
153
|
+
const total = event.lengthComputable ? event.total : file.size;
|
|
154
|
+
options.onProgress(event.loaded, total);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
xhr.onload = () => {
|
|
158
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
159
|
+
resolve({ path: normalized });
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
reject(parseXhrError(xhr));
|
|
163
|
+
};
|
|
164
|
+
xhr.onerror = () => {
|
|
165
|
+
reject(new StorageHttpError('Network error', 0));
|
|
166
|
+
};
|
|
167
|
+
xhr.onabort = () => {
|
|
168
|
+
reject(new StorageHttpError('Upload cancelled', 499, 'cancelled'));
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
if (options.signal) {
|
|
172
|
+
if (options.signal.aborted) {
|
|
173
|
+
xhr.abort();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
options.signal.addEventListener(
|
|
177
|
+
'abort',
|
|
178
|
+
() => {
|
|
179
|
+
xhr.abort();
|
|
180
|
+
},
|
|
181
|
+
{ once: true },
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
xhr.send(file);
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function uploadObject(
|
|
190
|
+
storageBaseUrl: string,
|
|
191
|
+
accessToken: string,
|
|
192
|
+
bucket: string,
|
|
193
|
+
path: string,
|
|
194
|
+
file: Blob,
|
|
195
|
+
options: { upsert?: boolean; contentType?: string } = {},
|
|
196
|
+
): Promise<{ path: string; id?: string }> {
|
|
197
|
+
const normalized = normalizeStoragePath(path);
|
|
198
|
+
const headers = new Headers();
|
|
199
|
+
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
200
|
+
headers.set('Content-Type', options.contentType || file.type || 'application/octet-stream');
|
|
201
|
+
if (options.upsert) headers.set('x-upsert', 'true');
|
|
202
|
+
|
|
203
|
+
const response = await fetch(objectUrl(storageBaseUrl, bucket, normalized), {
|
|
204
|
+
method: 'POST',
|
|
205
|
+
headers,
|
|
206
|
+
body: file,
|
|
207
|
+
});
|
|
208
|
+
if (!response.ok) {
|
|
209
|
+
throw await parseError(response);
|
|
210
|
+
}
|
|
211
|
+
let id: string | undefined;
|
|
212
|
+
const contentType = response.headers.get('Content-Type') || '';
|
|
213
|
+
if (contentType.includes('application/json')) {
|
|
214
|
+
try {
|
|
215
|
+
const body = (await response.json()) as { Id?: string; id?: string };
|
|
216
|
+
id = body.Id || body.id;
|
|
217
|
+
} catch {
|
|
218
|
+
/* ignore */
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return id ? { path: normalized, id } : { path: normalized };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export async function downloadObject(
|
|
225
|
+
storageBaseUrl: string,
|
|
226
|
+
accessToken: string,
|
|
227
|
+
bucket: string,
|
|
228
|
+
path: string,
|
|
229
|
+
): Promise<Blob> {
|
|
230
|
+
const response = await fetch(objectUrl(storageBaseUrl, bucket, path), {
|
|
231
|
+
headers: { Authorization: `Bearer ${accessToken}` },
|
|
232
|
+
});
|
|
233
|
+
if (!response.ok) {
|
|
234
|
+
throw await parseError(response);
|
|
235
|
+
}
|
|
236
|
+
return response.blob();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export async function moveObject(
|
|
240
|
+
storageBaseUrl: string,
|
|
241
|
+
accessToken: string,
|
|
242
|
+
bucket: string,
|
|
243
|
+
fromPath: string,
|
|
244
|
+
toPath: string,
|
|
245
|
+
): Promise<{ path: string }> {
|
|
246
|
+
const from = normalizeStoragePath(fromPath);
|
|
247
|
+
const to = normalizeStoragePath(toPath);
|
|
248
|
+
await request(storageBaseUrl, '/storage/v1/object/move', accessToken, {
|
|
249
|
+
method: 'POST',
|
|
250
|
+
body: JSON.stringify({
|
|
251
|
+
from: `${bucket}/${from}`,
|
|
252
|
+
to: `${bucket}/${to}`,
|
|
253
|
+
}),
|
|
254
|
+
});
|
|
255
|
+
return { path: to };
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export async function moveFolder(
|
|
259
|
+
storageBaseUrl: string,
|
|
260
|
+
accessToken: string,
|
|
261
|
+
bucket: string,
|
|
262
|
+
fromPath: string,
|
|
263
|
+
toPath: string,
|
|
264
|
+
): Promise<unknown> {
|
|
265
|
+
const from = normalizeStoragePath(fromPath);
|
|
266
|
+
const to = normalizeStoragePath(toPath);
|
|
267
|
+
return request(
|
|
268
|
+
storageBaseUrl,
|
|
269
|
+
`/storage/v1/object/prefix/${encodeURIComponent(bucket)}`,
|
|
270
|
+
accessToken,
|
|
271
|
+
{
|
|
272
|
+
method: 'POST',
|
|
273
|
+
body: JSON.stringify({ from, to }),
|
|
274
|
+
},
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export async function removeObjects(
|
|
279
|
+
storageBaseUrl: string,
|
|
280
|
+
accessToken: string,
|
|
281
|
+
bucket: string,
|
|
282
|
+
paths: string[],
|
|
283
|
+
): Promise<{ path: string }[]> {
|
|
284
|
+
const removed: { path: string }[] = [];
|
|
285
|
+
for (const path of paths) {
|
|
286
|
+
const normalized = normalizeStoragePath(path);
|
|
287
|
+
await request(
|
|
288
|
+
storageBaseUrl,
|
|
289
|
+
`/storage/v1/object/${encodeObjectPath(bucket, normalized)}`,
|
|
290
|
+
accessToken,
|
|
291
|
+
{
|
|
292
|
+
method: 'DELETE',
|
|
293
|
+
},
|
|
294
|
+
);
|
|
295
|
+
removed.push({ path: normalized });
|
|
296
|
+
}
|
|
297
|
+
return removed;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
export async function removeFolder(
|
|
301
|
+
storageBaseUrl: string,
|
|
302
|
+
accessToken: string,
|
|
303
|
+
bucket: string,
|
|
304
|
+
path: string,
|
|
305
|
+
): Promise<unknown> {
|
|
306
|
+
return request(
|
|
307
|
+
storageBaseUrl,
|
|
308
|
+
`/storage/v1/object/prefix/${encodeURIComponent(bucket)}`,
|
|
309
|
+
accessToken,
|
|
310
|
+
{
|
|
311
|
+
method: 'DELETE',
|
|
312
|
+
body: JSON.stringify({ prefix: normalizeStoragePath(path) }),
|
|
313
|
+
},
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export async function createFolder(
|
|
318
|
+
storageBaseUrl: string,
|
|
319
|
+
accessToken: string,
|
|
320
|
+
bucket: string,
|
|
321
|
+
path: string,
|
|
322
|
+
): Promise<{ path: string; id?: string }> {
|
|
323
|
+
const folderPath = normalizeStoragePath(path);
|
|
324
|
+
const markerPath = folderPath ? `${folderPath}/${FOLDER_PLACEHOLDER}` : FOLDER_PLACEHOLDER;
|
|
325
|
+
const marker = new Blob([], { type: 'application/x-directory' });
|
|
326
|
+
const uploaded = await uploadObject(storageBaseUrl, accessToken, bucket, markerPath, marker, {
|
|
327
|
+
upsert: true,
|
|
328
|
+
contentType: 'application/x-directory',
|
|
329
|
+
});
|
|
330
|
+
return { path: folderPath, ...(uploaded.id ? { id: uploaded.id } : {}) };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export async function getStorageItem(
|
|
334
|
+
storageBaseUrl: string,
|
|
335
|
+
accessToken: string,
|
|
336
|
+
objectId: string,
|
|
337
|
+
): Promise<unknown> {
|
|
338
|
+
return request(
|
|
339
|
+
storageBaseUrl,
|
|
340
|
+
`/storage/v1/object/id/${encodeURIComponent(objectId)}`,
|
|
341
|
+
accessToken,
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export async function folderStats(
|
|
346
|
+
storageBaseUrl: string,
|
|
347
|
+
accessToken: string,
|
|
348
|
+
bucket: string,
|
|
349
|
+
path: string,
|
|
350
|
+
): Promise<unknown> {
|
|
351
|
+
const prefix = encodeURIComponent(normalizeStoragePath(path));
|
|
352
|
+
return request(
|
|
353
|
+
storageBaseUrl,
|
|
354
|
+
`/storage/v1/object/prefix/${encodeURIComponent(bucket)}?prefix=${prefix}`,
|
|
355
|
+
accessToken,
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export async function executeStorageOp(
|
|
360
|
+
storageBaseUrl: string,
|
|
361
|
+
accessToken: string,
|
|
362
|
+
payload: StorageRequestInput | StorageRequestPayload,
|
|
363
|
+
): Promise<unknown> {
|
|
364
|
+
switch (payload.op) {
|
|
365
|
+
case 'listBuckets':
|
|
366
|
+
return listBuckets(storageBaseUrl, accessToken);
|
|
367
|
+
case 'list':
|
|
368
|
+
return listObjects(
|
|
369
|
+
storageBaseUrl,
|
|
370
|
+
accessToken,
|
|
371
|
+
payload.bucket,
|
|
372
|
+
payload.prefix,
|
|
373
|
+
payload.options,
|
|
374
|
+
);
|
|
375
|
+
case 'upload':
|
|
376
|
+
return uploadObject(storageBaseUrl, accessToken, payload.bucket, payload.path, payload.file, {
|
|
377
|
+
upsert: payload.upsert,
|
|
378
|
+
contentType: payload.contentType,
|
|
379
|
+
});
|
|
380
|
+
case 'download':
|
|
381
|
+
return downloadObject(storageBaseUrl, accessToken, payload.bucket, payload.path);
|
|
382
|
+
case 'move':
|
|
383
|
+
if (payload.folder) {
|
|
384
|
+
return moveFolder(
|
|
385
|
+
storageBaseUrl,
|
|
386
|
+
accessToken,
|
|
387
|
+
payload.bucket,
|
|
388
|
+
payload.fromPath,
|
|
389
|
+
payload.toPath,
|
|
390
|
+
);
|
|
391
|
+
}
|
|
392
|
+
return moveObject(
|
|
393
|
+
storageBaseUrl,
|
|
394
|
+
accessToken,
|
|
395
|
+
payload.bucket,
|
|
396
|
+
payload.fromPath,
|
|
397
|
+
payload.toPath,
|
|
398
|
+
);
|
|
399
|
+
case 'remove':
|
|
400
|
+
return removeObjects(storageBaseUrl, accessToken, payload.bucket, payload.paths);
|
|
401
|
+
case 'removeFolder':
|
|
402
|
+
return removeFolder(storageBaseUrl, accessToken, payload.bucket, payload.path);
|
|
403
|
+
case 'createFolder':
|
|
404
|
+
return createFolder(storageBaseUrl, accessToken, payload.bucket, payload.path);
|
|
405
|
+
case 'folderStats':
|
|
406
|
+
return folderStats(storageBaseUrl, accessToken, payload.bucket, payload.path);
|
|
407
|
+
case 'get':
|
|
408
|
+
return getStorageItem(storageBaseUrl, accessToken, payload.objectId);
|
|
409
|
+
default: {
|
|
410
|
+
const unknownOp = (payload as { op?: string }).op ?? 'unknown';
|
|
411
|
+
throw new StorageHttpError(`Unknown storage operation: ${unknownOp}`, 400);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { handleStorageRequest } from './handleRequest';
|
|
3
|
+
|
|
4
|
+
describe('handleStorageRequest', () => {
|
|
5
|
+
it('returns 503 when storage is not configured', async () => {
|
|
6
|
+
await expect(
|
|
7
|
+
handleStorageRequest({
|
|
8
|
+
storageUrl: null,
|
|
9
|
+
accessToken: 'token',
|
|
10
|
+
payload: { id: 'req-1', op: 'listBuckets' },
|
|
11
|
+
}),
|
|
12
|
+
).resolves.toEqual({
|
|
13
|
+
id: 'req-1',
|
|
14
|
+
error: { message: 'Storage is not configured', status: 503 },
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('returns 401 when the user is not signed in', async () => {
|
|
19
|
+
await expect(
|
|
20
|
+
handleStorageRequest({
|
|
21
|
+
storageUrl: 'http://localhost:8001',
|
|
22
|
+
accessToken: null,
|
|
23
|
+
payload: { id: 'req-2', op: 'listBuckets' },
|
|
24
|
+
}),
|
|
25
|
+
).resolves.toEqual({
|
|
26
|
+
id: 'req-2',
|
|
27
|
+
error: { message: 'Not authenticated', status: 401 },
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('returns 400 for an invalid payload', async () => {
|
|
32
|
+
await expect(
|
|
33
|
+
handleStorageRequest({
|
|
34
|
+
storageUrl: 'http://localhost:8001',
|
|
35
|
+
accessToken: 'token',
|
|
36
|
+
payload: { id: '', op: 'listBuckets' },
|
|
37
|
+
}),
|
|
38
|
+
).resolves.toMatchObject({
|
|
39
|
+
error: { status: 400 },
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('returns data from a successful operation', async () => {
|
|
44
|
+
vi.stubGlobal(
|
|
45
|
+
'fetch',
|
|
46
|
+
vi.fn().mockResolvedValue({
|
|
47
|
+
ok: true,
|
|
48
|
+
status: 200,
|
|
49
|
+
headers: new Headers({ 'Content-Type': 'application/json' }),
|
|
50
|
+
json: async () => [{ name: 'company' }],
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
await expect(
|
|
54
|
+
handleStorageRequest({
|
|
55
|
+
storageUrl: 'http://localhost:8001',
|
|
56
|
+
accessToken: 'token',
|
|
57
|
+
payload: { id: 'req-3', op: 'listBuckets' },
|
|
58
|
+
}),
|
|
59
|
+
).resolves.toEqual({
|
|
60
|
+
id: 'req-3',
|
|
61
|
+
data: [{ name: 'company' }],
|
|
62
|
+
});
|
|
63
|
+
vi.unstubAllGlobals();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('maps storage-service errors onto the response payload', async () => {
|
|
67
|
+
vi.stubGlobal(
|
|
68
|
+
'fetch',
|
|
69
|
+
vi.fn().mockResolvedValue({
|
|
70
|
+
ok: false,
|
|
71
|
+
status: 403,
|
|
72
|
+
statusText: 'Forbidden',
|
|
73
|
+
json: async () => ({ message: 'Denied' }),
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
76
|
+
await expect(
|
|
77
|
+
handleStorageRequest({
|
|
78
|
+
storageUrl: 'http://localhost:8001',
|
|
79
|
+
accessToken: 'token',
|
|
80
|
+
payload: { id: 'req-4', op: 'listBuckets' },
|
|
81
|
+
}),
|
|
82
|
+
).resolves.toEqual({
|
|
83
|
+
id: 'req-4',
|
|
84
|
+
error: { message: 'Denied', status: 403 },
|
|
85
|
+
});
|
|
86
|
+
vi.unstubAllGlobals();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
StorageErrorPayload,
|
|
3
|
+
StorageRequestPayload,
|
|
4
|
+
StorageResponsePayload,
|
|
5
|
+
} from '@shellui/sdk';
|
|
6
|
+
import { executeStorageOp, StorageHttpError } from './api';
|
|
7
|
+
|
|
8
|
+
export type HandleStorageRequestInput = {
|
|
9
|
+
storageUrl: string | null;
|
|
10
|
+
accessToken: string | null;
|
|
11
|
+
payload: StorageRequestPayload;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export async function handleStorageRequest({
|
|
15
|
+
storageUrl,
|
|
16
|
+
accessToken,
|
|
17
|
+
payload,
|
|
18
|
+
}: HandleStorageRequestInput): Promise<StorageResponsePayload> {
|
|
19
|
+
if (!payload?.id || !payload.op) {
|
|
20
|
+
return {
|
|
21
|
+
id: payload?.id ?? '',
|
|
22
|
+
error: { message: 'Invalid storage request', status: 400 },
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!storageUrl) {
|
|
27
|
+
return {
|
|
28
|
+
id: payload.id,
|
|
29
|
+
error: { message: 'Storage is not configured', status: 503 },
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!accessToken) {
|
|
34
|
+
return {
|
|
35
|
+
id: payload.id,
|
|
36
|
+
error: { message: 'Not authenticated', status: 401 },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
const data = await executeStorageOp(storageUrl, accessToken, payload);
|
|
42
|
+
return { id: payload.id, data };
|
|
43
|
+
} catch (err) {
|
|
44
|
+
const error: StorageErrorPayload =
|
|
45
|
+
err instanceof StorageHttpError
|
|
46
|
+
? { message: err.message, status: err.status, code: err.code }
|
|
47
|
+
: {
|
|
48
|
+
message: err instanceof Error ? err.message : 'Request failed',
|
|
49
|
+
status: 500,
|
|
50
|
+
};
|
|
51
|
+
return { id: payload.id, error };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import type { ShellUIConfig } from '../config/types';
|
|
3
|
+
import {
|
|
4
|
+
fetchStorageQuota,
|
|
5
|
+
formatBytes,
|
|
6
|
+
getStorageBaseUrl,
|
|
7
|
+
getUsagePercent,
|
|
8
|
+
isStorageSettingsEnabled,
|
|
9
|
+
parseStorageQuota,
|
|
10
|
+
} from './quota';
|
|
11
|
+
|
|
12
|
+
describe('getStorageBaseUrl', () => {
|
|
13
|
+
it('returns a trimmed url without a trailing slash', () => {
|
|
14
|
+
expect(getStorageBaseUrl({ storage: { url: ' http://localhost:8001/ ' } })).toBe(
|
|
15
|
+
'http://localhost:8001',
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('returns null when storage is missing or empty', () => {
|
|
20
|
+
expect(getStorageBaseUrl(undefined)).toBeNull();
|
|
21
|
+
expect(getStorageBaseUrl({} as ShellUIConfig)).toBeNull();
|
|
22
|
+
expect(getStorageBaseUrl({ storage: { url: ' ' } })).toBeNull();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('isStorageSettingsEnabled', () => {
|
|
27
|
+
it('is true when storage.url is set', () => {
|
|
28
|
+
expect(isStorageSettingsEnabled({ storage: { url: 'http://localhost:8001' } })).toBe(true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('is false when storage is not configured', () => {
|
|
32
|
+
expect(isStorageSettingsEnabled(undefined)).toBe(false);
|
|
33
|
+
expect(isStorageSettingsEnabled({} as ShellUIConfig)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('is false when showInSettings is false', () => {
|
|
37
|
+
expect(
|
|
38
|
+
isStorageSettingsEnabled({
|
|
39
|
+
storage: { url: 'http://localhost:8001', showInSettings: false },
|
|
40
|
+
}),
|
|
41
|
+
).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('is true when showInSettings is true', () => {
|
|
45
|
+
expect(
|
|
46
|
+
isStorageSettingsEnabled({
|
|
47
|
+
storage: { url: 'http://localhost:8001', showInSettings: true },
|
|
48
|
+
}),
|
|
49
|
+
).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('formatBytes', () => {
|
|
54
|
+
it('formats zero and invalid values as 0 B', () => {
|
|
55
|
+
expect(formatBytes(0)).toBe('0 B');
|
|
56
|
+
expect(formatBytes(-1)).toBe('0 B');
|
|
57
|
+
expect(formatBytes(Number.NaN)).toBe('0 B');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('formats bytes, kilobytes, and gigabytes', () => {
|
|
61
|
+
expect(formatBytes(512)).toBe('512 B');
|
|
62
|
+
expect(formatBytes(1536)).toBe('1.5 KB');
|
|
63
|
+
expect(formatBytes(10 * 1024)).toBe('10 KB');
|
|
64
|
+
expect(formatBytes(1024 ** 3)).toBe('1 GB');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('getUsagePercent', () => {
|
|
69
|
+
it('returns null when there is no positive max', () => {
|
|
70
|
+
expect(getUsagePercent(100, null)).toBeNull();
|
|
71
|
+
expect(getUsagePercent(100, 0)).toBeNull();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('clamps used bytes between 0 and 100', () => {
|
|
75
|
+
expect(getUsagePercent(0, 100)).toBe(0);
|
|
76
|
+
expect(getUsagePercent(25, 100)).toBe(25);
|
|
77
|
+
expect(getUsagePercent(200, 100)).toBe(100);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
describe('parseStorageQuota', () => {
|
|
82
|
+
const validPayload = {
|
|
83
|
+
company_id: 1,
|
|
84
|
+
company: { max_bytes: 1000, used_bytes: 250, remaining_bytes: 750 },
|
|
85
|
+
user: { user_id: 42, max_bytes: null, used_bytes: 80, remaining_bytes: null },
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
it('parses a valid quota payload', () => {
|
|
89
|
+
expect(parseStorageQuota(validPayload)).toEqual({
|
|
90
|
+
companyId: 1,
|
|
91
|
+
company: { maxBytes: 1000, usedBytes: 250, remainingBytes: 750 },
|
|
92
|
+
user: { userId: 42, maxBytes: null, usedBytes: 80, remainingBytes: null },
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('treats a zero max as no limit', () => {
|
|
97
|
+
expect(
|
|
98
|
+
parseStorageQuota({
|
|
99
|
+
...validPayload,
|
|
100
|
+
user: { user_id: 42, max_bytes: 0, used_bytes: 80, remaining_bytes: null },
|
|
101
|
+
})?.user.maxBytes,
|
|
102
|
+
).toBeNull();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('returns null for incomplete payloads', () => {
|
|
106
|
+
expect(parseStorageQuota(null)).toBeNull();
|
|
107
|
+
expect(parseStorageQuota({})).toBeNull();
|
|
108
|
+
expect(parseStorageQuota({ ...validPayload, user: { used_bytes: 1 } })).toBeNull();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
describe('fetchStorageQuota', () => {
|
|
113
|
+
afterEach(() => {
|
|
114
|
+
vi.unstubAllGlobals();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('requests the quota endpoint with a bearer token', async () => {
|
|
118
|
+
const fetchMock = vi.fn().mockResolvedValue({
|
|
119
|
+
ok: true,
|
|
120
|
+
json: async () => ({
|
|
121
|
+
company_id: 1,
|
|
122
|
+
company: { max_bytes: 100, used_bytes: 10, remaining_bytes: 90 },
|
|
123
|
+
user: { user_id: 2, max_bytes: 50, used_bytes: 5, remaining_bytes: 45 },
|
|
124
|
+
}),
|
|
125
|
+
});
|
|
126
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
127
|
+
|
|
128
|
+
await expect(fetchStorageQuota('http://localhost:8001/', 'token-1')).resolves.toMatchObject({
|
|
129
|
+
companyId: 1,
|
|
130
|
+
user: { userId: 2, usedBytes: 5 },
|
|
131
|
+
});
|
|
132
|
+
expect(fetchMock).toHaveBeenCalledWith('http://localhost:8001/storage/v1/quota', {
|
|
133
|
+
headers: {
|
|
134
|
+
Authorization: 'Bearer token-1',
|
|
135
|
+
Accept: 'application/json',
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('throws when the response is not ok', async () => {
|
|
141
|
+
vi.stubGlobal(
|
|
142
|
+
'fetch',
|
|
143
|
+
vi.fn().mockResolvedValue({
|
|
144
|
+
ok: false,
|
|
145
|
+
status: 401,
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
await expect(fetchStorageQuota('http://localhost:8001', 'token-1')).rejects.toThrow('HTTP 401');
|
|
150
|
+
});
|
|
151
|
+
});
|