-
Notifications
You must be signed in to change notification settings - Fork 13k
feat: new endpoint to delete uploaded files individually #38173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f8361ae
feat: new endpoint to delete uploaded files individually
pierre-lehnen-rc adb1e8b
Merge branch 'develop' into feat/uploads.delete
pierre-lehnen-rc edf096b
fix types
pierre-lehnen-rc 0d5d671
use updateMessage
pierre-lehnen-rc f9af4fd
fail if an app block the update
pierre-lehnen-rc 22d1d90
renamed findAllByOriginalId to findAllByOriginalFileId
pierre-lehnen-rc 91f4b72
use bodyParams
pierre-lehnen-rc 77efa19
use logger from APIClass
pierre-lehnen-rc 5cbc624
move endpoint code to the service
pierre-lehnen-rc 39aa243
use a constant
pierre-lehnen-rc 0085ab5
Merge branch 'develop' into feat/uploads.delete
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,10 @@ | ||
| --- | ||
| '@rocket.chat/core-services': minor | ||
| '@rocket.chat/model-typings': minor | ||
| '@rocket.chat/core-typings': minor | ||
| '@rocket.chat/models': minor | ||
| '@rocket.chat/i18n': minor | ||
| '@rocket.chat/meteor': minor | ||
| --- | ||
|
|
||
| Adds a new endpoint to delete uploaded files individually |
This file contains hidden or 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 hidden or 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 hidden or 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,99 @@ | ||
| import { Upload } from '@rocket.chat/core-services'; | ||
| import type { IUpload } from '@rocket.chat/core-typings'; | ||
| import { Messages, Uploads, Users } from '@rocket.chat/models'; | ||
| import { | ||
| ajv, | ||
| validateBadRequestErrorResponse, | ||
| validateUnauthorizedErrorResponse, | ||
| validateForbiddenErrorResponse, | ||
| validateNotFoundErrorResponse, | ||
| } from '@rocket.chat/rest-typings'; | ||
|
|
||
| import type { ExtractRoutesFromAPI } from '../ApiClass'; | ||
| import { API } from '../api'; | ||
|
|
||
| type UploadsDeleteResult = { | ||
| /** | ||
| * The list of files that were successfully removed; May include additional files such as image thumbnails | ||
| * */ | ||
| deletedFiles: IUpload['_id'][]; | ||
| }; | ||
|
|
||
| type UploadsDeleteParams = { | ||
| fileId: string; | ||
| }; | ||
|
|
||
| const uploadsDeleteParamsSchema = { | ||
| type: 'object', | ||
| properties: { | ||
| fileId: { | ||
| type: 'string', | ||
| }, | ||
| }, | ||
| required: ['fileId'], | ||
| additionalProperties: false, | ||
| }; | ||
|
|
||
| export const isUploadsDeleteParams = ajv.compile<UploadsDeleteParams>(uploadsDeleteParamsSchema); | ||
|
|
||
| const uploadsDeleteEndpoint = API.v1.post( | ||
KevLehman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 'uploads.delete', | ||
| { | ||
| authRequired: true, | ||
| body: isUploadsDeleteParams, | ||
| response: { | ||
| 400: validateBadRequestErrorResponse, | ||
| 401: validateUnauthorizedErrorResponse, | ||
| 403: validateForbiddenErrorResponse, | ||
| 404: validateNotFoundErrorResponse, | ||
| 200: ajv.compile<UploadsDeleteResult>({ | ||
| type: 'object', | ||
| properties: { | ||
| success: { | ||
| type: 'boolean', | ||
| }, | ||
| deletedFiles: { | ||
| description: 'The list of files that were successfully removed. May include additional files such as image thumbnails', | ||
| type: 'array', | ||
| items: { | ||
| type: 'string', | ||
| }, | ||
| }, | ||
| }, | ||
| required: ['deletedFiles'], | ||
| additionalProperties: false, | ||
| }), | ||
| }, | ||
| }, | ||
| async function action() { | ||
| const { fileId } = this.bodyParams; | ||
|
|
||
| const file = await Uploads.findOneById(fileId); | ||
| if (!file?.userId || !file.rid) { | ||
| return API.v1.notFound(); | ||
| } | ||
|
|
||
| const msg = await Messages.getMessageByFileId(fileId); | ||
| if (!(await Upload.canDeleteFile(this.userId, file, msg))) { | ||
| return API.v1.forbidden('forbidden'); | ||
| } | ||
|
|
||
| const user = await Users.findOneById(this.userId); | ||
| // Safeguard, can't really happen | ||
| if (!user) { | ||
| return API.v1.forbidden('forbidden'); | ||
| } | ||
|
|
||
| const { deletedFiles } = await Upload.deleteFile(user, fileId, msg); | ||
| return API.v1.success({ | ||
| deletedFiles, | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| type UploadsEndpoints = ExtractRoutesFromAPI<typeof uploadsDeleteEndpoint>; | ||
|
|
||
| declare module '@rocket.chat/rest-typings' { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface | ||
| interface Endpoints extends UploadsEndpoints {} | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 const NOTIFICATION_ATTACHMENT_COLOR = '#FD745E'; |
This file contains hidden or 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 hidden or 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
14 changes: 10 additions & 4 deletions
14
packages/core-typings/src/IMessage/MessageAttachment/Files/FileAttachmentProps.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,13 +1,19 @@ | ||
| import type { MessageAttachmentBase } from '../MessageAttachmentBase'; | ||
| import type { AudioAttachmentProps } from './AudioAttachmentProps'; | ||
| import type { FileProp } from './FileProp'; | ||
| import type { ImageAttachmentProps } from './ImageAttachmentProps'; | ||
| import type { VideoAttachmentProps } from './VideoAttachmentProps'; | ||
|
|
||
| type CommonFileProps = { | ||
| type: 'file'; | ||
| fileId?: FileProp['_id']; | ||
| }; | ||
|
|
||
| export type FileAttachmentProps = | ||
| | ({ type: 'file' } & VideoAttachmentProps) | ||
| | ({ type: 'file' } & ImageAttachmentProps) | ||
| | ({ type: 'file' } & AudioAttachmentProps) | ||
| | ({ type: 'file' } & MessageAttachmentBase); | ||
| | (CommonFileProps & VideoAttachmentProps) | ||
| | (CommonFileProps & ImageAttachmentProps) | ||
| | (CommonFileProps & AudioAttachmentProps) | ||
| | (CommonFileProps & MessageAttachmentBase); | ||
|
|
||
| export const isFileAttachment = (attachment: MessageAttachmentBase): attachment is FileAttachmentProps => | ||
| 'type' in attachment && (attachment as any).type === 'file'; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.