-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
volar
committed
Nov 16, 2023
1 parent
9e81c05
commit 517be98
Showing
8 changed files
with
512 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { AxiosInstance } from 'axios' | ||
import type { DocId } from '@/types/common' | ||
import { SYSTEM_CORE_DAM } from '@/services/api/coreDam/assetApi' | ||
import type { AssetDetailItemDto } from '@/types/coreDam/Asset' | ||
import { apiFetchOne } from '@/services/api/apiFetchOne' | ||
|
||
const END_POINT = '/adm/v1/asset' | ||
const ENTITY = 'asset' | ||
|
||
export const fetchAsset = (client: () => AxiosInstance, id: DocId) => | ||
apiFetchOne<AssetDetailItemDto>(client, END_POINT + '/:id', { id }, SYSTEM_CORE_DAM, ENTITY) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { AxiosInstance } from 'axios' | ||
import { type UploadQueueItem, UploadQueueItemType } from '@/types/coreDam/UploadQueue' | ||
import type { DocId } from '@/types/common' | ||
import { HTTP_STATUS_CREATED, HTTP_STATUS_OK } from '@/composables/statusCodes' | ||
import { damFileTypeFix } from '@/components/file/composables/fileType' | ||
|
||
const END_POINT = '/adm/v1/image' | ||
const CHUNK_UPLOAD_TIMEOUT = 420 | ||
|
||
export const imageUploadStart = (client: () => AxiosInstance, item: UploadQueueItem) => { | ||
return new Promise((resolve, reject) => { | ||
let url = END_POINT + '/licence/' + item.licenceId | ||
if (item.type === UploadQueueItemType.SlotFile && item.slotName && item.assetId) { | ||
url = END_POINT + '/asset/' + item.assetId + '/slot-name/' + item.slotName | ||
} | ||
client() | ||
.post( | ||
url, | ||
JSON.stringify({ | ||
mimeType: damFileTypeFix(item.file), | ||
size: item.file?.size, | ||
}) | ||
) | ||
.then((res) => { | ||
if (res.status === HTTP_STATUS_CREATED) { | ||
resolve(res.data) | ||
} else { | ||
// | ||
reject() | ||
} | ||
}) | ||
.catch((err) => { | ||
// | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
|
||
export const imageUploadChunk = ( | ||
client: (timeout?: number) => AxiosInstance, | ||
item: UploadQueueItem, | ||
imageId: DocId, | ||
buffer: string, | ||
size: number, | ||
offset: number, | ||
onUploadProgressCallback: ((progressEvent: any) => void) | undefined = undefined | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
const formData = new FormData() | ||
const url = END_POINT + '/' + imageId + '/chunk' | ||
formData.append('file', buffer) | ||
formData.append( | ||
'chunk', | ||
JSON.stringify({ | ||
offset: offset, | ||
size: size, | ||
}) | ||
) | ||
|
||
client(CHUNK_UPLOAD_TIMEOUT) | ||
.post(url, formData, { | ||
cancelToken: | ||
item.chunks[item.currentChunkIndex] && item.chunks[item.currentChunkIndex].cancelTokenSource | ||
? item.chunks[item.currentChunkIndex].cancelTokenSource.token | ||
: undefined, | ||
headers: { | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
onUploadProgress: onUploadProgressCallback, | ||
}) | ||
.then((res) => { | ||
if (res.status === HTTP_STATUS_CREATED) { | ||
resolve(res.data) | ||
} else { | ||
// | ||
reject() | ||
} | ||
}) | ||
.catch((err) => { | ||
// | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
|
||
export const imageUploadFinish = (client: () => AxiosInstance, item: UploadQueueItem, sha: string) => { | ||
return new Promise((resolve, reject) => { | ||
const url = END_POINT + '/' + item.fileId + '/uploaded' | ||
client() | ||
.patch( | ||
url, | ||
JSON.stringify({ | ||
checksum: sha, | ||
}) | ||
) | ||
.then((res) => { | ||
if (res.status === HTTP_STATUS_OK) { | ||
resolve(res.data) | ||
} else { | ||
// | ||
reject() | ||
} | ||
}) | ||
.catch((err) => { | ||
// | ||
reject(err) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { DamAssetType } from '@/types/coreDam/Asset' | ||
import { type DamUploadStartResponse, type UploadQueueItem, UploadQueueItemStatus } from '@/types/coreDam/UploadQueue' | ||
import { imageUploadChunk, imageUploadFinish, imageUploadStart } from '@/components/dam/uploadQueue/api/damImageApi' | ||
import type { AxiosInstance } from 'axios' | ||
import type { DocId } from '@/types/common' | ||
import { AssetFileProcessStatus } from '@/types/coreDam/AssetFile' | ||
import { fetchAsset } from '@/components/dam/uploadQueue/api/damAssetApi' | ||
import { useUploadQueuesStore } from '@/components/dam/uploadQueue/uploadQueuesStore' | ||
|
||
const NOTIFICATION_FALLBACK_TIMER_CHECK_SECONDS = 10 | ||
const NOTIFICATION_FALLBACK_MAX_TRIES = 3 | ||
|
||
export const damUploadStart: (client: () => AxiosInstance, item: UploadQueueItem) => Promise<DamUploadStartResponse> = ( | ||
client: () => AxiosInstance, | ||
item: UploadQueueItem | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
if (item.assetType !== DamAssetType.Image) { | ||
reject() | ||
return | ||
} | ||
imageUploadStart(client, item) | ||
.then((res) => { | ||
resolve(res as DamUploadStartResponse) | ||
return | ||
}) | ||
.catch((err) => reject(err)) | ||
}) | ||
} | ||
|
||
export const damUploadChunk = ( | ||
client: () => AxiosInstance, | ||
item: UploadQueueItem, | ||
imageId: DocId, | ||
buffer: string, | ||
size: number, | ||
offset: number, | ||
onUploadProgressCallback: any | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
if (item.assetType !== DamAssetType.Image) { | ||
reject() | ||
return | ||
} | ||
imageUploadChunk(client, item, imageId, buffer, size, offset, onUploadProgressCallback) | ||
.then((res) => { | ||
resolve(res) | ||
}) | ||
.catch((err) => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
|
||
export const damUploadFinish = ( | ||
client: () => AxiosInstance, | ||
item: UploadQueueItem, | ||
sha: string, | ||
uploadStatusFallback: boolean | ||
) => { | ||
return new Promise((resolve, reject) => { | ||
if (item.assetType !== DamAssetType.Image) { | ||
reject() | ||
return | ||
} | ||
imageUploadFinish(client, item, sha) | ||
.then((res) => { | ||
item.status = UploadQueueItemStatus.Processing | ||
if (uploadStatusFallback) { | ||
item.notificationFallbackTimer = setTimeout(function () { | ||
notificationFallbackCallback(client, item) | ||
}, calculateFallbackTime(item)) | ||
} | ||
resolve(res) | ||
}) | ||
.catch((err) => reject(err)) | ||
}) | ||
} | ||
|
||
function calculateFallbackTime(item: UploadQueueItem) { | ||
return NOTIFICATION_FALLBACK_TIMER_CHECK_SECONDS * 1000 * item.notificationFallbackTry * item.notificationFallbackTry | ||
} | ||
|
||
async function notificationFallbackCallback(client: () => AxiosInstance, item: UploadQueueItem) { | ||
clearTimeout(item.notificationFallbackTimer) | ||
if (item.status === UploadQueueItemStatus.Uploaded) return | ||
if (item.notificationFallbackTry > NOTIFICATION_FALLBACK_MAX_TRIES) return | ||
if (!item.assetId) return | ||
const asset = await fetchAsset(client, item.assetId) | ||
if (asset && asset.mainFile && asset.mainFile.fileAttributes) { | ||
const uploadQueuesStore = useUploadQueuesStore() | ||
if (asset.mainFile.fileAttributes.status === AssetFileProcessStatus.Processed) { | ||
uploadQueuesStore.queueItemProcessed(asset.id) | ||
return | ||
} else if (asset.mainFile.fileAttributes.status === AssetFileProcessStatus.Duplicate) { | ||
uploadQueuesStore.queueItemDuplicate(asset.id) | ||
return | ||
} else if (asset.mainFile.fileAttributes.status === AssetFileProcessStatus.Failed) { | ||
uploadQueuesStore.queueItemFailed(asset.id, asset.mainFile.fileAttributes.failReason) | ||
return | ||
} | ||
} | ||
item.notificationFallbackTry++ | ||
item.notificationFallbackTimer = setTimeout(function () { | ||
notificationFallbackCallback(client, item) | ||
}, calculateFallbackTime(item)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.