From e739b8b5eec4ed26a69cdb3dfe9f6e1ef2d45d56 Mon Sep 17 00:00:00 2001 From: Aaditya Jaiswal Date: Thu, 16 Apr 2026 15:23:52 +0530 Subject: [PATCH] loal storage rename form 16 --- src/services/gcsStorage.service.ts | 47 +++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/services/gcsStorage.service.ts b/src/services/gcsStorage.service.ts index e694767..480ecd8 100644 --- a/src/services/gcsStorage.service.ts +++ b/src/services/gcsStorage.service.ts @@ -24,6 +24,28 @@ class GCSStorageService { private bucketName: string = ''; private projectId: string = ''; + private getAbsoluteUploadPath(relativePath: string): string { + return path.join(UPLOAD_DIR, ...relativePath.replace(/\\/g, '/').split('/').filter(Boolean)); + } + + private renameLocalStoredFile(oldNorm: string, newRelativePath: string, newFileName: string): { + storageUrl: string; + filePath: string; + fileName: string; + } { + const fullOld = this.getAbsoluteUploadPath(oldNorm); + const fullNew = this.getAbsoluteUploadPath(newRelativePath); + if (!fs.existsSync(fullOld)) { + throw new Error(`Local file not found: ${oldNorm}`); + } + fs.mkdirSync(path.dirname(fullNew), { recursive: true }); + fs.renameSync(fullOld, fullNew); + const normalizedPath = newRelativePath.replace(/\\/g, '/'); + const storageUrl = `/uploads/${normalizedPath}`; + logger.info('[GCS] Renamed local Form 16 document', { oldNorm, newRelativePath: normalizedPath }); + return { storageUrl, filePath: normalizedPath, fileName: newFileName }; + } + constructor() { // Check if Google Secret Manager should be used const useGoogleSecretManager = process.env.USE_GOOGLE_SECRET_MANAGER === 'true'; @@ -373,19 +395,24 @@ class GCSStorageService { const oldNorm = oldRelativePath.replace(/\\/g, '/'); const dir = path.posix.dirname(oldNorm); const newRelativePath = `${dir}/${newFileName}`; + const localOldExists = fs.existsSync(this.getAbsoluteUploadPath(oldNorm)); + logger.info('[GCS] renameRequestDocumentFile storage presence check', { + oldNorm, + newRelativePath, + localOldExists, + gcsConfigured: this.isConfigured(), + }); if (!this.isConfigured()) { logger.info('[GCS] renameRequestDocumentFile using local storage mode'); - const fullOld = path.join(UPLOAD_DIR, ...oldNorm.split('/').filter(Boolean)); - const fullNew = path.join(UPLOAD_DIR, ...newRelativePath.split('/').filter(Boolean)); - if (!fs.existsSync(fullOld)) { - throw new Error(`Local file not found: ${oldNorm}`); - } - fs.mkdirSync(path.dirname(fullNew), { recursive: true }); - fs.renameSync(fullOld, fullNew); - const storageUrl = `/uploads/${newRelativePath.replace(/\\/g, '/')}`; - logger.info('[GCS] Renamed local Form 16 document', { oldNorm, newRelativePath }); - return { storageUrl, filePath: newRelativePath.replace(/\\/g, '/'), fileName: newFileName }; + return this.renameLocalStoredFile(oldNorm, newRelativePath, newFileName); + } + + // Important: if upload previously fell back to local storage, rename must also happen locally + // even when GCS is configured in the current environment. + if (localOldExists) { + logger.info('[GCS] renameRequestDocumentFile detected local-stored file; using local rename'); + return this.renameLocalStoredFile(oldNorm, newRelativePath, newFileName); } if (!this.storage) {