-
Notifications
You must be signed in to change notification settings - Fork 0
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 #49 from cieslarmichal/feature/multiple-files-support
add support for multiple files upload
- Loading branch information
Showing
16 changed files
with
163 additions
and
132 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
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 |
---|---|---|
@@ -1,17 +1,15 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { type Readable } from 'node:stream'; | ||
|
||
export interface AttachedFile { | ||
readonly name: string; | ||
readonly type: string; | ||
readonly data: Readable; | ||
readonly filePath: string; | ||
} | ||
|
||
export interface HttpRequest<Body = any, QueryParams = any, PathParams = any> { | ||
readonly body: Body; | ||
readonly queryParams: QueryParams; | ||
readonly pathParams: PathParams; | ||
readonly headers: Record<string, string>; | ||
readonly file?: AttachedFile | undefined; | ||
readonly files?: AttachedFile[] | undefined; | ||
} |
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
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
13 changes: 0 additions & 13 deletions
13
.../application/commandHandlers/uploadResourceCommandHandler/uploadResourceCommandHandler.ts
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
...lication/commandHandlers/uploadResourceCommandHandler/uploadResourceCommandHandlerImpl.ts
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...pplication/commandHandlers/uploadResourcesCommandHandler/uploadResourcesCommandHandler.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,10 @@ | ||
import { type CommandHandler } from '../../../../../common/types/commandHandler.js'; | ||
import { type AttachedFile } from '../../../../../common/types/http/httpRequest.js'; | ||
|
||
export interface UploadResourcesCommandHandlerPayload { | ||
readonly userId: string; | ||
readonly bucketName: string; | ||
readonly files: AttachedFile[]; | ||
} | ||
|
||
export type UploadResourcesCommandHandler = CommandHandler<UploadResourcesCommandHandlerPayload, void>; |
73 changes: 73 additions & 0 deletions
73
...cation/commandHandlers/uploadResourcesCommandHandler/uploadResourcesCommandHandlerImpl.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,73 @@ | ||
import mime from 'mime'; | ||
import { createReadStream } from 'node:fs'; | ||
|
||
import { | ||
type UploadResourcesCommandHandler, | ||
type UploadResourcesCommandHandlerPayload, | ||
} from './uploadResourcesCommandHandler.js'; | ||
import { OperationNotValidError } from '../../../../../common/errors/common/operationNotValidError.js'; | ||
import { type LoggerService } from '../../../../../libs/logger/services/loggerService/loggerService.js'; | ||
import { type FindUserBucketsQueryHandler } from '../../../../userModule/application/queryHandlers/findUserBucketsQueryHandler/findUserBucketsQueryHandler.js'; | ||
import { type ResourceBlobService } from '../../../domain/services/resourceBlobService/resourceBlobService.js'; | ||
|
||
export class UploadResourcesCommandHandlerImpl implements UploadResourcesCommandHandler { | ||
public constructor( | ||
private readonly resourceBlobSerice: ResourceBlobService, | ||
private readonly loggerService: LoggerService, | ||
private readonly findUserBucketsQueryHandler: FindUserBucketsQueryHandler, | ||
) {} | ||
|
||
public async execute(payload: UploadResourcesCommandHandlerPayload): Promise<void> { | ||
const { userId, bucketName, files } = payload; | ||
|
||
const { buckets } = await this.findUserBucketsQueryHandler.execute({ userId }); | ||
|
||
if (!buckets.includes(bucketName)) { | ||
throw new OperationNotValidError({ | ||
reason: 'Bucket is not assigned to this user.', | ||
userId, | ||
bucketName, | ||
}); | ||
} | ||
|
||
await Promise.all( | ||
files.map(async (file) => { | ||
const { name: resourceName, filePath } = file; | ||
|
||
const contentType = mime.getType(filePath); | ||
|
||
if (!contentType) { | ||
throw new OperationNotValidError({ | ||
reason: 'Content type not found', | ||
resourceName, | ||
}); | ||
} | ||
|
||
this.loggerService.debug({ | ||
message: 'Uploading Resource...', | ||
userId, | ||
bucketName, | ||
resourceName, | ||
contentType, | ||
}); | ||
|
||
const data = createReadStream(filePath); | ||
|
||
await this.resourceBlobSerice.uploadResource({ | ||
bucketName, | ||
resourceName, | ||
data, | ||
contentType, | ||
}); | ||
|
||
this.loggerService.debug({ | ||
message: 'Resource uploaded.', | ||
userId, | ||
bucketName, | ||
resourceName, | ||
contentType, | ||
}); | ||
}), | ||
); | ||
} | ||
} |
Oops, something went wrong.