refactor: stage uploads before drive and git delivery
This commit is contained in:
+51
-15
@@ -10,6 +10,12 @@ export interface CheckResult {
|
||||
diffs: FileDiff[]
|
||||
}
|
||||
|
||||
export interface StageResult {
|
||||
stagingId: string
|
||||
folderName: string
|
||||
filesCount: number
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Shared FormData builder
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -49,15 +55,17 @@ function buildUploadFormData(
|
||||
* Throws on auth/network errors so callers can surface them to the user.
|
||||
*/
|
||||
export async function checkFolderDiffs(
|
||||
folder: FolderEntry,
|
||||
stagingId: string,
|
||||
secret: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<CheckResult> {
|
||||
const formData = buildUploadFormData(folder)
|
||||
const res = await fetch('/api/upload/check', {
|
||||
method: 'POST',
|
||||
headers: { 'x-upload-secret': secret.trim() },
|
||||
body: formData,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-upload-secret': secret.trim(),
|
||||
},
|
||||
body: JSON.stringify({ stagingId }),
|
||||
signal,
|
||||
})
|
||||
|
||||
@@ -75,24 +83,51 @@ export async function checkFolderDiffs(
|
||||
return { exists: true, diffs: (data.diffs || []) as FileDiff[] }
|
||||
}
|
||||
|
||||
export async function stageUpload(
|
||||
folder: FolderEntry,
|
||||
secret: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<StageResult> {
|
||||
const formData = buildUploadFormData(folder)
|
||||
const res = await fetch('/api/upload/stage', {
|
||||
method: 'POST',
|
||||
headers: { 'x-upload-secret': secret.trim() },
|
||||
body: formData,
|
||||
signal,
|
||||
})
|
||||
|
||||
const data = await res.json()
|
||||
|
||||
if (!res.ok || !data.success) {
|
||||
throw new Error(data.error || `Erreur serveur (${res.status})`)
|
||||
}
|
||||
|
||||
return {
|
||||
stagingId: data.stagingId,
|
||||
folderName: data.folderName,
|
||||
filesCount: data.filesCount,
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Upload original files to Nextcloud Drive
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/** Upload original files to Nextcloud Drive (no Blender compression). */
|
||||
export async function uploadDrive(
|
||||
folder: FolderEntry,
|
||||
stagingId: string,
|
||||
secret: string,
|
||||
action: 'new' | 'replace',
|
||||
signal?: AbortSignal,
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
const formData = buildUploadFormData(folder, { action })
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/upload/drive', {
|
||||
method: 'POST',
|
||||
headers: { 'x-upload-secret': secret.trim() },
|
||||
body: formData,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-upload-secret': secret.trim(),
|
||||
},
|
||||
body: JSON.stringify({ stagingId, action }),
|
||||
signal,
|
||||
})
|
||||
const data = await res.json()
|
||||
@@ -112,20 +147,21 @@ export async function uploadDrive(
|
||||
|
||||
/** Upload files to GitHub (with Blender compression). */
|
||||
export async function uploadGit(
|
||||
folder: FolderEntry,
|
||||
stagingId: string,
|
||||
secret: string,
|
||||
onProgress: (pct: number) => void,
|
||||
signal?: AbortSignal,
|
||||
): Promise<{ success: boolean; filename?: string; error?: string }> {
|
||||
const formData = buildUploadFormData(folder)
|
||||
|
||||
onProgress(10)
|
||||
|
||||
try {
|
||||
const res = await fetch('/api/upload/git', {
|
||||
method: 'POST',
|
||||
headers: { 'x-upload-secret': secret.trim() },
|
||||
body: formData,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-upload-secret': secret.trim(),
|
||||
},
|
||||
body: JSON.stringify({ stagingId }),
|
||||
signal,
|
||||
})
|
||||
|
||||
@@ -137,7 +173,7 @@ export async function uploadGit(
|
||||
}
|
||||
|
||||
onProgress(100)
|
||||
return { success: true, filename: folder.folderName }
|
||||
return { success: true, filename: data.folderName }
|
||||
} catch (err) {
|
||||
if (err instanceof DOMException && err.name === 'AbortError') {
|
||||
return { success: false, error: 'Upload annule' }
|
||||
|
||||
Reference in New Issue
Block a user