refactor: clean upload pipeline and restore draco delivery
This commit is contained in:
+23
-25
@@ -1,31 +1,37 @@
|
||||
import { isRecord } from './guards'
|
||||
import type { FolderEntry } from './client-types'
|
||||
import type { FileDiff } from './types'
|
||||
import type { DriveAction, FileDiff, GitModelMode, StagingUploadResult } from './types'
|
||||
|
||||
export interface CheckResult {
|
||||
exists: boolean
|
||||
diffs: FileDiff[]
|
||||
}
|
||||
|
||||
interface StageResult {
|
||||
stagingId: string
|
||||
folderName: string
|
||||
filesCount: number
|
||||
}
|
||||
|
||||
function getApiError(data: unknown, fallback: string) {
|
||||
return isRecord(data) && typeof data.error === 'string' ? data.error : fallback
|
||||
}
|
||||
|
||||
function getUploadJsonHeaders(secret: string) {
|
||||
return {
|
||||
'Content-Type': 'application/json',
|
||||
'x-upload-secret': secret.trim(),
|
||||
}
|
||||
}
|
||||
|
||||
function isAbortError(err: unknown) {
|
||||
return err instanceof DOMException && err.name === 'AbortError'
|
||||
}
|
||||
|
||||
function isFileDiff(value: unknown): value is FileDiff {
|
||||
return isRecord(value)
|
||||
&& typeof value.name === 'string'
|
||||
&& (value.status === 'new' || value.status === 'changed' || value.status === 'deleted')
|
||||
}
|
||||
|
||||
function buildUploadFormData(folder: FolderEntry): FormData {
|
||||
function buildUploadFormData(folder: FolderEntry, gitModelMode: GitModelMode): FormData {
|
||||
const formData = new FormData()
|
||||
formData.append('folderName', folder.folderName)
|
||||
formData.append('gitModelMode', gitModelMode)
|
||||
|
||||
formData.append('files', folder.modelFile)
|
||||
formData.append('fileTypes', 'model')
|
||||
@@ -47,10 +53,7 @@ export async function checkFolderDiffs(
|
||||
): Promise<CheckResult> {
|
||||
const res = await fetch('/api/upload/check', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-upload-secret': secret.trim(),
|
||||
},
|
||||
headers: getUploadJsonHeaders(secret),
|
||||
body: JSON.stringify({ stagingId }),
|
||||
signal,
|
||||
})
|
||||
@@ -72,10 +75,11 @@ export async function checkFolderDiffs(
|
||||
|
||||
export async function stageUpload(
|
||||
folder: FolderEntry,
|
||||
gitModelMode: GitModelMode,
|
||||
secret: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<StageResult> {
|
||||
const formData = buildUploadFormData(folder)
|
||||
): Promise<StagingUploadResult> {
|
||||
const formData = buildUploadFormData(folder, gitModelMode)
|
||||
const res = await fetch('/api/upload/stage', {
|
||||
method: 'POST',
|
||||
headers: { 'x-upload-secret': secret.trim() },
|
||||
@@ -103,16 +107,13 @@ export async function stageUpload(
|
||||
export async function uploadDrive(
|
||||
stagingId: string,
|
||||
secret: string,
|
||||
action: 'new' | 'replace',
|
||||
action: DriveAction,
|
||||
signal?: AbortSignal,
|
||||
): Promise<{ success: boolean; error?: string }> {
|
||||
try {
|
||||
const res = await fetch('/api/upload/drive', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-upload-secret': secret.trim(),
|
||||
},
|
||||
headers: getUploadJsonHeaders(secret),
|
||||
body: JSON.stringify({ stagingId, action }),
|
||||
signal,
|
||||
})
|
||||
@@ -122,7 +123,7 @@ export async function uploadDrive(
|
||||
}
|
||||
return { success: true }
|
||||
} catch (err) {
|
||||
if (err instanceof DOMException && err.name === 'AbortError') {
|
||||
if (isAbortError(err)) {
|
||||
return { success: false, error: 'Upload annule' }
|
||||
}
|
||||
return { success: false, error: 'Erreur reseau (Drive)' }
|
||||
@@ -140,10 +141,7 @@ export async function uploadGit(
|
||||
try {
|
||||
const res = await fetch('/api/upload/git', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-upload-secret': secret.trim(),
|
||||
},
|
||||
headers: getUploadJsonHeaders(secret),
|
||||
body: JSON.stringify({ stagingId }),
|
||||
signal,
|
||||
})
|
||||
@@ -158,7 +156,7 @@ export async function uploadGit(
|
||||
onProgress(100)
|
||||
return { success: true, filename: typeof data.folderName === 'string' ? data.folderName : undefined }
|
||||
} catch (err) {
|
||||
if (err instanceof DOMException && err.name === 'AbortError') {
|
||||
if (isAbortError(err)) {
|
||||
return { success: false, error: 'Upload annule' }
|
||||
}
|
||||
return { success: false, error: 'Erreur reseau' }
|
||||
|
||||
Reference in New Issue
Block a user