Signed url complete (#5)

Reviewed-on: #5
Reviewed-by: Benoy Bose <benoybose@cosq.net>
Co-authored-by: DhanshCOSQ <dhanshas@cosq.net>
Co-committed-by: DhanshCOSQ <dhanshas@cosq.net>
This commit is contained in:
Dhansh A S 2025-04-05 09:20:27 +00:00 committed by Benoy Bose
parent f9930b143c
commit f00ff4d1d6

View File

@ -9,7 +9,7 @@ import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import * as https from 'https'; import * as https from 'https';
import axios from "axios"; import axios from "axios";
import { getStorage } from 'firebase-admin/storage';
const formData = require('form-data'); const formData = require('form-data');
const Mailgun = require('mailgun.js'); const Mailgun = require('mailgun.js');
const { convert } = require('html-to-text'); const { convert } = require('html-to-text');
@ -79,6 +79,42 @@ export const sendEmailWithAttachment = onRequest({
response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) }); response.status(500).json({ success: false, error: error instanceof Error ? error.message : String(error) });
} }
}); });
export const accessFile = onRequest({
region: '#{SERVICES_RGN}#'
}, async (request: Request, response: express.Response) => {
try {
const filePath = request.query.path as string;
if (!filePath) {
response.status(400).send('File path is required');
return;
}
const expirationMs = 60 * 60 * 1000;
const bucket = getStorage().bucket();
const file = bucket.file(filePath);
const [exists] = await file.exists();
if (!exists) {
response.status(404).send('File not found');
return;
}
const [signedUrl] = await file.getSignedUrl({
action: 'read',
expires: Date.now() + expirationMs,
responseDisposition: `attachment; filename="${path.basename(filePath)}"`,
});
response.redirect(signedUrl);
logger.info(`File access redirect for ${filePath}`);
} catch (error) {
logger.error('Error accessing file:', error);
response.status(500).send('Error accessing file');
}
});
export const sendEmailMessage = onRequest({ export const sendEmailMessage = onRequest({
region: process.env.SERVICES_RGN region: process.env.SERVICES_RGN
}, (request: Request, response: express.Response) => { }, (request: Request, response: express.Response) => {