From 4979402dab7bd3f603c025dd27082bef0a5ab296 Mon Sep 17 00:00:00 2001 From: Konstantinos Kopanidis Date: Wed, 29 May 2024 14:32:24 +0300 Subject: [PATCH] feat(storage): introduce gRPC call to get a file's URL (#1045) --- .../grpc-sdk/src/modules/storage/index.ts | 5 +++ modules/storage/src/Storage.ts | 31 +++++++++++++++++++ modules/storage/src/storage.proto | 10 ++++++ 3 files changed, 46 insertions(+) diff --git a/libraries/grpc-sdk/src/modules/storage/index.ts b/libraries/grpc-sdk/src/modules/storage/index.ts index 43f605b3a..f1866fb32 100644 --- a/libraries/grpc-sdk/src/modules/storage/index.ts +++ b/libraries/grpc-sdk/src/modules/storage/index.ts @@ -3,6 +3,7 @@ import { DeleteFileResponse, FileResponse, GetFileDataResponse, + GetFileUrlResponse, StorageDefinition, } from '../../protoUtils/storage.js'; @@ -20,6 +21,10 @@ export class Storage extends ConduitModule { return this.client!.getFile({ id, userId, scope }); } + getFileUrl(id: string, userId?: string, scope?: string): Promise { + return this.client!.getFileUrl({ id, userId, scope }); + } + getFileData(id: string, userId?: string, scope?: string): Promise { return this.client!.getFileData({ id, userId, scope }); } diff --git a/modules/storage/src/Storage.ts b/modules/storage/src/Storage.ts index 37b3344c8..06e6b0976 100644 --- a/modules/storage/src/Storage.ts +++ b/modules/storage/src/Storage.ts @@ -22,6 +22,8 @@ import { FileResponse, GetFileDataResponse, GetFileRequest, + GetFileUrlRequest, + GetFileUrlResponse, UpdateFileByUrlRequest, UpdateFileRequest, } from './protoTypes/storage.js'; @@ -38,6 +40,7 @@ import { StorageParamAdapter } from './adapter/StorageParamAdapter.js'; import { FileResource } from './authz/index.js'; import { AdminFileHandlers } from './admin/adminFile.js'; import { fileURLToPath } from 'node:url'; + const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -54,6 +57,7 @@ export default class Storage extends ManagedModule { deleteFile: this.deleteFile.bind(this), createFileByUrl: this.createFileByUrl.bind(this), updateFileByUrl: this.updateFileByUrl.bind(this), + getFileUrl: this.getFileUrl.bind(this), }, }; protected metricsSchema = MetricsSchema; @@ -203,6 +207,33 @@ export default class Storage extends ManagedModule { callback(null, result as GetFileDataResponse); } + async getFileUrl( + call: GrpcRequest, + callback: GrpcCallback, + ) { + if (!this._adminFileHandlers) + return callback({ + code: status.INTERNAL, + message: 'File handlers not initiated', + }); + const request = createParsedRouterRequest( + call.request, + undefined, + { scope: call.request.scope }, + undefined, + undefined, + undefined, + { user: { _id: call.request.userId } }, + ); + let result; + if (call.request.scope || call.request.userId) { + result = await this._fileHandlers.getFileUrl(request); + } else { + result = await this._adminFileHandlers.getFileUrl(request); + } + callback(null, result as GetFileUrlResponse); + } + async createFile( call: GrpcRequest, callback: GrpcCallback, diff --git a/modules/storage/src/storage.proto b/modules/storage/src/storage.proto index e2e89ba6f..6dc57138a 100644 --- a/modules/storage/src/storage.proto +++ b/modules/storage/src/storage.proto @@ -7,6 +7,15 @@ message GetFileRequest { optional string scope = 3; } +message GetFileUrlRequest { + string id = 1; + optional string userId = 2; + optional string scope = 3; + +} +message GetFileUrlResponse { + string url = 1; +} message CreateFileRequest { string name = 1; @@ -75,6 +84,7 @@ message UpdateFileByUrlRequest { service Storage { rpc GetFile(GetFileRequest) returns (FileResponse); rpc GetFileData(GetFileRequest) returns (GetFileDataResponse); + rpc GetFileUrl(GetFileUrlRequest) returns (GetFileUrlResponse); rpc UpdateFile(UpdateFileRequest) returns (FileResponse); rpc CreateFile(CreateFileRequest) returns (FileResponse); rpc DeleteFile(GetFileRequest) returns (DeleteFileResponse);