mirror of
https://github.com/OpenSignLabs/OpenSign.git
synced 2026-08-25 17:12:37 +02:00
Merge pull request
This commit is contained in:
@@ -1,39 +1,63 @@
|
||||
import AWS from 'aws-sdk';
|
||||
import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
|
||||
import { getSignedUrl as presign } from '@aws-sdk/s3-request-presigner';
|
||||
import { useLocal } from '../../Utils.js';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import dotenv from 'dotenv';
|
||||
import { isAuthenticated } from '../../utils/AuthUtils.js';
|
||||
dotenv.config({ quiet: true });
|
||||
|
||||
export default function getPresignedUrl(url) {
|
||||
function extractKeyFromUrl(url) {
|
||||
// Create a new URL object
|
||||
const parsedUrl = new URL(url);
|
||||
// Get the pathname of the URL
|
||||
const pathname = parsedUrl.pathname; // e.g. /mybucket/path/to/file.pdf (depends on baseUrl style)
|
||||
// Extract the filename from the pathname
|
||||
const filename = pathname.substring(pathname.lastIndexOf('/') + 1);
|
||||
return filename;
|
||||
}
|
||||
|
||||
function makeEndpoint(endpoint) {
|
||||
if (!endpoint) return '';
|
||||
|
||||
if (endpoint.startsWith('http://') || endpoint.startsWith('https://')) {
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
return `https://${endpoint}`;
|
||||
}
|
||||
|
||||
function makeS3Client() {
|
||||
const accessKeyId = process.env.DO_ACCESS_KEY_ID;
|
||||
|
||||
const secretAccessKey = process.env.DO_SECRET_ACCESS_KEY;
|
||||
|
||||
const region = process.env.DO_REGION;
|
||||
|
||||
const endpoint = makeEndpoint(process.env.DO_ENDPOINT);
|
||||
|
||||
return new S3Client({
|
||||
region,
|
||||
endpoint, // endpoint should be Url e.g. https://blr1.digitaloceanspaces.com)
|
||||
credentials: { accessKeyId, secretAccessKey },
|
||||
});
|
||||
}
|
||||
|
||||
export default async function getPresignedUrl(url) {
|
||||
if (url?.includes('files')) {
|
||||
return presignedlocalUrl(url);
|
||||
} else {
|
||||
const credentials = {
|
||||
accessKeyId: process.env.DO_ACCESS_KEY_ID,
|
||||
secretAccessKey: process.env.DO_SECRET_ACCESS_KEY,
|
||||
};
|
||||
AWS.config.update({
|
||||
credentials: credentials,
|
||||
region: process.env.DO_REGION,
|
||||
});
|
||||
const spacesEndpoint = new AWS.Endpoint(process.env.DO_ENDPOINT);
|
||||
const client = makeS3Client();
|
||||
|
||||
const s3 = new AWS.S3({ endpoint: spacesEndpoint, signatureVersion: 'v4' });
|
||||
const bucket = process.env.DO_SPACE;
|
||||
|
||||
// Create a new URL object
|
||||
const parsedUrl = new URL(url);
|
||||
// Get the pathname of the URL
|
||||
const pathname = parsedUrl.pathname;
|
||||
// Extract the filename from the pathname
|
||||
const filename = pathname.substring(pathname.lastIndexOf('/') + 1);
|
||||
const key = extractKeyFromUrl(url);
|
||||
|
||||
const command = new GetObjectCommand({ Bucket: bucket, Key: key });
|
||||
// Expires: 160 seconds
|
||||
const expiresIn = 160;
|
||||
|
||||
// presignedGETURL return presignedUrl with expires time
|
||||
const presignedGETURL = s3.getSignedUrl('getObject', {
|
||||
Bucket: process.env.DO_SPACE,
|
||||
Key: filename, //filename
|
||||
Expires: 160, //time to expire in seconds
|
||||
});
|
||||
const presignedGETURL = await presign(client, command, { expiresIn });
|
||||
return presignedGETURL;
|
||||
}
|
||||
}
|
||||
@@ -68,7 +92,7 @@ export async function getSignedUrl(request) {
|
||||
}
|
||||
}
|
||||
|
||||
const presignedUrl = getPresignedUrl(url);
|
||||
const presignedUrl = await getPresignedUrl(url);
|
||||
return presignedUrl;
|
||||
} else {
|
||||
return url;
|
||||
@@ -85,7 +109,7 @@ export async function getSignedUrl(request) {
|
||||
if (url?.includes('files')) {
|
||||
return presignedlocalUrl(url);
|
||||
} else if (useLocal !== 'true') {
|
||||
const presignedUrl = getPresignedUrl(url);
|
||||
const presignedUrl = await getPresignedUrl(url);
|
||||
return presignedUrl;
|
||||
} else {
|
||||
return url;
|
||||
|
||||
Reference in New Issue
Block a user