-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8689 from weseek/support/commonize-upload-attachm…
…ents-operation support: Commonize upload-attachments operation
- Loading branch information
Showing
7 changed files
with
91 additions
and
66 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 @@ | ||
export * from './upload-attachments'; |
39 changes: 39 additions & 0 deletions
39
apps/app/src/client/services/upload-attachments/upload-attachments.ts
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,39 @@ | ||
import type { IAttachment } from '@growi/core'; | ||
|
||
import { apiv3Get, apiv3PostForm } from '~/client/util/apiv3-client'; | ||
import type { IApiv3GetAttachmentLimitParams, IApiv3GetAttachmentLimitResponse, IApiv3PostAttachmentResponse } from '~/interfaces/apiv3/attachment'; | ||
import loggerFactory from '~/utils/logger'; | ||
|
||
|
||
const logger = loggerFactory('growi:client:services:upload-attachment'); | ||
|
||
|
||
type UploadOpts = { | ||
onUploaded?: (attachment: IAttachment) => void, | ||
onError?: (error: Error, file: File) => void, | ||
} | ||
|
||
export const uploadAttachments = async(pageId: string, files: File[], opts?: UploadOpts): Promise<void> => { | ||
files.forEach(async(file) => { | ||
try { | ||
const params: IApiv3GetAttachmentLimitParams = { fileSize: file.size }; | ||
const { data: resLimit } = await apiv3Get<IApiv3GetAttachmentLimitResponse>('/attachment/limit', params); | ||
|
||
if (!resLimit.isUploadable) { | ||
throw new Error(resLimit.errorMessage); | ||
} | ||
|
||
const formData = new FormData(); | ||
formData.append('file', file); | ||
formData.append('page_id', pageId); | ||
|
||
const { data: resAdd } = await apiv3PostForm<IApiv3PostAttachmentResponse>('/attachment', formData); | ||
|
||
opts?.onUploaded?.(resAdd.attachment); | ||
} | ||
catch (e) { | ||
logger.error('failed to upload', e); | ||
opts?.onError?.(e, file); | ||
} | ||
}); | ||
}; |
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
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
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,15 @@ | ||
import type { IAttachment, IPage, IRevision } from '@growi/core'; | ||
|
||
import type { ICheckLimitResult } from '../attachment'; | ||
|
||
export type IApiv3GetAttachmentLimitParams = { | ||
fileSize: number, | ||
}; | ||
|
||
export type IApiv3GetAttachmentLimitResponse = ICheckLimitResult; | ||
|
||
export type IApiv3PostAttachmentResponse = { | ||
page: IPage, | ||
revision: IRevision, | ||
attachment: IAttachment, | ||
} |
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
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