fix: refactoring, adding express api.

This commit is contained in:
charlesgauthereau
2026-01-31 22:15:01 +01:00
parent 7c5d57ddc1
commit cc87cf68b5
33 changed files with 1028 additions and 142 deletions
@@ -24,18 +24,27 @@ export async function uploadGoogleDrive(
? await ensureFolderPath(client, folderPath, config.folderId)
: config.folderId;
const existing = await findFileByName(client, fileName, folderId);
if (existing) return {success: false, provider: "google-drive", error: "File already exists"};
let fileStream: Readable;
const file = input.data.file;
if (Buffer.isBuffer(file) || file instanceof Uint8Array) {
fileStream = Readable.from(file);
} else if ((file as any).pipe) {
fileStream = file as Readable;
} else {
throw new Error("Unsupported file type for streaming upload");
}
await client.files.create({
requestBody: {name: fileName, parents: [folderId]},
media: {body: Readable.from(input.data.file as Buffer)},
media: {body: fileStream},
fields: "id",
supportsAllDrives: true,
});
if (input.data.url) {
const url = await generateFileUrl(input);
if (!url) {
@@ -68,11 +77,15 @@ export async function getGoogleDrive(
const fileId = await resolveFilePath(client, input.data.path, config.folderId);
if (!fileId) return {success: false, provider: "google-drive", error: "File not found"};
const res = await client.files.get(
{fileId, alt: "media", supportsAllDrives: true},
{responseType: "arraybuffer"}
{responseType: "stream"}
);
const stream = res.data as Readable;
if (input.data.signedUrl) {
const url = await generateFileUrl(input);
@@ -88,7 +101,7 @@ export async function getGoogleDrive(
return {
success: true,
provider: "google-drive",
file: Buffer.from(res.data as ArrayBuffer),
file: stream,
url: url,
};
}
@@ -96,7 +109,7 @@ export async function getGoogleDrive(
return {
success: true,
provider: "google-drive",
file: Buffer.from(res.data as ArrayBuffer),
file: stream,
};
}