loal storage rename form 16

This commit is contained in:
Aaditya Jaiswal 2026-04-16 15:23:52 +05:30
parent 1a02781731
commit e739b8b5ee

View File

@ -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}`);
return this.renameLocalStoredFile(oldNorm, newRelativePath, newFileName);
}
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 };
// 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) {