Skip to content

Commit

Permalink
Support fileName for media
Browse files Browse the repository at this point in the history
  • Loading branch information
hakonkrogh committed Dec 27, 2022
1 parent 8573d55 commit d0429eb
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 12 deletions.
20 changes: 20 additions & 0 deletions .changeset/olive-games-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'@crystallize/import-utilities': minor
---

Support fileName for media input. This allows you to define what the final fileName for an image should be, instead of just using the source image url. Example json spec:

```
{
"items": [
...
"images": [
{
"src": "https://example.com/some-file.jpg",
"fileName": "my-own-name-here.jpg"
}
]
...
]
}
```
4 changes: 2 additions & 2 deletions src/bootstrap-tenant/bootstrapper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ export class Bootstrapper extends EventEmitter {
topicPathToIDMap: new Map<string, string>(),

fileUploader: new FileUploadManager(),
uploadFileFromUrl: (url: string) =>
this.context.fileUploader.uploadFromUrl(url),
uploadFileFromUrl: (url: string, fileName?: string) =>
this.context.fileUploader.uploadFromUrl(url, fileName),
callPIM: () => Promise.resolve({ data: {} }),
callCatalogue: () => Promise.resolve({ data: {} }),
callSearch: () => Promise.resolve({ data: {} }),
Expand Down
12 changes: 7 additions & 5 deletions src/bootstrap-tenant/bootstrapper/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ async function createImagesInput(

if (!key) {
try {
const uploadResult = await context.uploadFileFromUrl(image.src)
const uploadResult = await context.uploadFileFromUrl(
image.src,
image.fileName
)
if (uploadResult) {
key = uploadResult.key
mimeType = uploadResult.mimeType
Expand Down Expand Up @@ -1935,10 +1938,9 @@ export async function setItems({
if (itemRelationComponentIndex !== -1) {
chunk[
itemRelationComponentIndex
].itemRelations.itemIds =
await getItemIdsForItemRelation(
jsonChunk[itemRelationId] as JSONItemRelation[]
)
].itemRelations.itemIds = await getItemIdsForItemRelation(
jsonChunk[itemRelationId] as JSONItemRelation[]
)
}
})
)
Expand Down
19 changes: 15 additions & 4 deletions src/bootstrap-tenant/bootstrapper/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ export interface BootstrapperContext {
topicPathToIDMap: Map<string, string>
itemVersions: Map<string, ItemVersionsForLanguages>
fileUploader: FileUploadManager
uploadFileFromUrl: (url: string) => Promise<RemoteFileUploadResult | null>
uploadFileFromUrl: (
url: string,
fileName?: string
) => Promise<RemoteFileUploadResult | null>
callPIM: (props: IcallAPI) => Promise<IcallAPIResult>
callCatalogue: (props: IcallAPI) => Promise<IcallAPIResult>
callSearch: (props: IcallAPI) => Promise<IcallAPIResult>
Expand Down Expand Up @@ -151,11 +154,13 @@ export function getTranslation(translation?: any, language?: string): string {

type uploadFileRecord = {
url: string
fileName?: string
result: Promise<RemoteFileUploadResult | null>
}

type fileUploadQueueItem = {
url: string
fileName?: string
status: 'not-started' | 'working' | 'done'
failCount?: number
resolve?: (result: RemoteFileUploadResult | null) => void
Expand Down Expand Up @@ -202,6 +207,7 @@ export class FileUploadManager {
try {
const result = await remoteFileUpload({
fileUrl: item.url,
fileName: item.fileName,
context: this.context,
})

Expand Down Expand Up @@ -234,28 +240,33 @@ export class FileUploadManager {
}
}

uploadFromUrl(url: string) {
uploadFromUrl(url: string, fileName?: string) {
const existing = this.uploads.find((u) => u.url === url)
if (existing) {
return existing.result
}

const result = this.scheduleUpload(url)
const result = this.scheduleUpload(url, fileName)

result.catch(() => ({}))

this.uploads.push({
url,
fileName,
result,
})

return result
}

scheduleUpload(url: string): Promise<RemoteFileUploadResult | null> {
scheduleUpload(
url: string,
fileName?: string
): Promise<RemoteFileUploadResult | null> {
return new Promise((resolve, reject) => {
this.workerQueue.push({
url,
fileName,
status: 'not-started',
resolve,
reject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ export async function remoteFileUpload({
if (fileUrl) {
const downloadResult = await downloadFile(fileUrl)
file = downloadResult.file
fileName = downloadResult.filename
if (!fileName) {
fileName = downloadResult.filename
}
contentType = downloadResult.contentType
} else if (file && !contentType) {
const result = await handleFileBuffer(file)
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap-tenant/json-spec/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export type JSONRichTextTranslated = JSONRichText | Record<string, JSONRichText>
export interface JSONMedia {
src: string
key?: string
fileName?: string
}

export interface JSONImage extends JSONMedia {
Expand Down

0 comments on commit d0429eb

Please sign in to comment.