Skip to content

Commit

Permalink
fix(upload-client): do not request fileInfo before creating group fro…
Browse files Browse the repository at this point in the history
…m uuid
  • Loading branch information
nd0ut committed Oct 18, 2023
1 parent 4df9e0c commit e3ac795
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 37 deletions.
6 changes: 5 additions & 1 deletion packages/upload-client/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ export type FileInfo = {
metadata?: Metadata
}

export type GroupFileInfo = FileInfo & {
defaultEffects: string | null
}

export type GroupInfo = {
datetimeCreated: string
datetimeStored: string | null
filesCount: string
cdnUrl: string
files: FileInfo[]
files: GroupFileInfo[]
url: string
id: GroupId
}
Expand Down
1 change: 1 addition & 0 deletions packages/upload-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export {
FileInfo,
GroupId,
GroupInfo,
GroupFileInfo,
Token,
Url,
Uuid,
Expand Down
14 changes: 12 additions & 2 deletions packages/upload-client/src/tools/UploadcareGroup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GroupId, GroupInfo } from '../api/types'
import defaultSettings from '../defaultSettings'
import { UploadcareFile } from './UploadcareFile'

export class UploadcareGroup {
Expand All @@ -12,7 +13,14 @@ export class UploadcareGroup {
readonly createdAt: string
readonly storedAt: string | null = null

constructor(groupInfo: GroupInfo, files: UploadcareFile[]) {
constructor(
groupInfo: GroupInfo,
{
baseCDN = defaultSettings.baseCDN
}: {
baseCDN?: string
} = {}
) {
this.uuid = groupInfo.id
this.filesCount = groupInfo.filesCount
this.totalSize = Object.values(groupInfo.files).reduce(
Expand All @@ -24,7 +32,9 @@ export class UploadcareGroup {
(file) => file.isImage
).length
this.cdnUrl = groupInfo.cdnUrl
this.files = files
this.files = groupInfo.files.map(
(fileInfo) => new UploadcareFile(fileInfo, { baseCDN })
)
this.createdAt = groupInfo.datetimeCreated
this.storedAt = groupInfo.datetimeStored
}
Expand Down
70 changes: 38 additions & 32 deletions packages/upload-client/src/uploadFileGroup/uploadFileGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
} from '../api/types'
import { SupportedFileInput } from '../types'
import { isFileDataArray, isUrlArray, isUuidArray } from './types'
import { isFileData } from '../tools/isFileData'
import { isUrl } from '../uploadFile/types'

export type GroupFromOptions = {
jsonpCallback?: string
Expand Down Expand Up @@ -80,39 +82,43 @@ export function uploadFileGroup(
}
}

return Promise.all<UploadcareFile>(
return Promise.all<Uuid>(
(data as SupportedFileInput[]).map(
(file: SupportedFileInput | Url | Uuid, index: number) =>
uploadFile(file, {
publicKey,

fileName,
baseURL,
secureSignature,
secureExpire,
store,

signal,
onProgress: createProgressHandler(filesCount, index),

source,
integration,
userAgent,

retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes,

contentType,
multipartChunkSize,

baseCDN,
checkForUrlDuplicates,
saveUrlForRecurrentUploads
})
(file: SupportedFileInput | Url | Uuid, index: number) => {
if (isFileData(file) || isUrl(file)) {
return uploadFile(file, {
publicKey,

fileName,
baseURL,
secureSignature,
secureExpire,
store,

signal,
onProgress: createProgressHandler(filesCount, index),

source,
integration,
userAgent,

retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes,

contentType,
multipartChunkSize,

baseCDN,
checkForUrlDuplicates,
saveUrlForRecurrentUploads
}).then((fileInfo) => fileInfo.uuid)
} else {
// Do not request file info by uuid before creating group because this isn't necessary
return file
}
}
)
).then((files) => {
const uuids = files.map((file) => file.uuid)

).then((uuids) => {
return group(uuids, {
publicKey,
baseURL,
Expand All @@ -126,7 +132,7 @@ export function uploadFileGroup(
retryThrottledRequestMaxTimes,
retryNetworkErrorMaxTimes
})
.then((groupInfo) => new UploadcareGroup(groupInfo, files))
.then((groupInfo) => new UploadcareGroup(groupInfo, { baseCDN }))
.then((group) => {
onProgress && onProgress({ isComputable: true, value: 1 })
return group
Expand Down
2 changes: 1 addition & 1 deletion packages/upload-client/test/_fixtureFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function groupOfFiles(id: string): Array<string> {
const groupOfFiles = {
valid: [
'392e3aa3-5ed6-4ad6-a67e-b3a7c1d5b9e9',
'49b4c5a1-31b3-4349-ba07-d97a2d883c37'
'49b4c5a1-31b3-4349-ba07-d97a2d883c37/-/resize/x800/'
],
invalid: [
'2e6b7f23-9143-4b71-94e7-338bb',
Expand Down
6 changes: 5 additions & 1 deletion packages/upload-client/test/api/group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ describe('API - group', () => {
publicKey: factory.publicKey('image')
})

it('should upload group of files', async () => {
it('should create group of files', async () => {
const data = await group(files, settings)

expect(data).toBeTruthy()
expect(data.id).toBeTruthy()
expect(data.files).toBeTruthy()
expect(data.files[0].uuid).toBe(files[0])

Check failure on line 18 in packages/upload-client/test/api/group.test.ts

View workflow job for this annotation

GitHub Actions / build

API - group › should create group of files

expect(received).toBe(expected) // Object.is equality Expected: "392e3aa3-5ed6-4ad6-a67e-b3a7c1d5b9e9" Received: "d3275f8b-686d-4980-916a-53a1fc17450b" at Object.<anonymous> (test/api/group.test.ts:18:32)
expect(data.files[0].defaultEffects).toBe(null)
expect(data.files[1].uuid).toBe(files[1])
expect(data.files[1].defaultEffects).toBe('resize/200x/')
})

it('should fail with [HTTP 400] No files[N] parameters found.', async () => {
Expand Down

0 comments on commit e3ac795

Please sign in to comment.