mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
feat: Working version with google drive storage.
This commit is contained in:
@@ -1,154 +1,71 @@
|
||||
"use server"
|
||||
import {StorageDeleteInput, StorageGetInput, StorageResult, StorageUploadInput} from '../../types';
|
||||
import {StorageDeleteInput, StorageGetInput, StorageMetaData, StorageResult, StorageUploadInput} from '../../types';
|
||||
import {GoogleDriveConfig} from "@/features/storages/providers/google-drive/types";
|
||||
import {findFileByName, getGoogleDriveClient} from "@/features/storages/providers/google-drive/helpers";
|
||||
import {
|
||||
ensureFolderPath,
|
||||
findFileByName,
|
||||
getGoogleDriveClient, resolveFilePath
|
||||
} from "@/features/storages/providers/google-drive/helpers";
|
||||
import {Readable} from "node:stream";
|
||||
import {generateFileUrl} from "@/features/storages/helpers";
|
||||
|
||||
|
||||
// export async function uploadGoogleDrive(
|
||||
// config: GoogleDriveConfig,
|
||||
// input: { data: StorageUploadInput }
|
||||
// ): Promise<StorageResult> {
|
||||
// const client = await getGoogleDriveClient(config);
|
||||
//
|
||||
// const name = input.data.path;
|
||||
//
|
||||
// const existing = await findFileByName(client, name, config.folderId);
|
||||
// if (existing) {
|
||||
// return {
|
||||
// success: false,
|
||||
// provider: "google-drive",
|
||||
// error: "File already exists"
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// await client.files.create({
|
||||
// requestBody: {
|
||||
// name,
|
||||
// parents: [config.folderId],
|
||||
// },
|
||||
// media: {
|
||||
// body: input.data.file as Buffer,
|
||||
// },
|
||||
// });
|
||||
//
|
||||
//
|
||||
// return {
|
||||
// success: true,
|
||||
// provider: 'google-drive',
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// export async function getGoogleDrive(
|
||||
// config: GoogleDriveConfig,
|
||||
// input: { data: StorageGetInput }
|
||||
// ): Promise<StorageResult> {
|
||||
// const client = await getGoogleDriveClient(config);
|
||||
// const name = input.data.path;
|
||||
//
|
||||
// const fileId = await findFileByName(client, name, config.folderId);
|
||||
// if (!fileId) {
|
||||
// return {success: false, provider: "google-drive", error: "File not found"};
|
||||
// }
|
||||
//
|
||||
// const res = await client.files.get(
|
||||
// {fileId, alt: "media"},
|
||||
// {responseType: "arraybuffer"}
|
||||
// );
|
||||
//
|
||||
// return {
|
||||
// success: true,
|
||||
// provider: "google-drive",
|
||||
// file: Buffer.from(res.data as ArrayBuffer),
|
||||
// };
|
||||
// }
|
||||
//
|
||||
//
|
||||
// export async function deleteGoogleDrive(
|
||||
// config: GoogleDriveConfig,
|
||||
// input: { data: StorageDeleteInput }
|
||||
// ): Promise<StorageResult> {
|
||||
// const client = await getGoogleDriveClient(config);
|
||||
// const name = input.data.path;
|
||||
//
|
||||
// const fileId = await findFileByName(client, name, config.folderId);
|
||||
// if (!fileId) {
|
||||
// return {success: false, provider: "google-drive", error: "File not found"};
|
||||
// }
|
||||
//
|
||||
// await client.files.delete({fileId});
|
||||
//
|
||||
// return {
|
||||
// success: true,
|
||||
// provider: "google-drive"
|
||||
// };
|
||||
// }
|
||||
//
|
||||
//
|
||||
// export async function pingGoogleDrive(config: GoogleDriveConfig): Promise<StorageResult> {
|
||||
// try {
|
||||
// const drive = await getGoogleDriveClient(config);
|
||||
// const name = `ping-${Date.now()}.txt`;
|
||||
//
|
||||
// const buffer = Buffer.from("ping");
|
||||
//
|
||||
// const file = await drive.files.create({
|
||||
// requestBody: {
|
||||
// name,
|
||||
// parents: [config.folderId],
|
||||
// },
|
||||
// media: {
|
||||
// mimeType: "text/plain",
|
||||
// body: Readable.from(buffer),
|
||||
// },
|
||||
// fields: "id",
|
||||
// });
|
||||
// console.log(file)
|
||||
//
|
||||
// await drive.files.get({fileId: file.data.id!});
|
||||
// await drive.files.delete({fileId: file.data.id!});
|
||||
//
|
||||
// return {
|
||||
// success: true,
|
||||
// provider: "google-drive",
|
||||
// response: "Google Drive storage OK",
|
||||
// };
|
||||
// } catch (err: any) {
|
||||
// return {
|
||||
// success: false,
|
||||
// provider: "google-drive",
|
||||
// response: err.message,
|
||||
// };
|
||||
// }
|
||||
// }
|
||||
export async function uploadGoogleDrive(
|
||||
config: GoogleDriveConfig,
|
||||
input: { data: StorageUploadInput }
|
||||
input: { data: StorageUploadInput, metadata?: StorageMetaData },
|
||||
): Promise<StorageResult> {
|
||||
const client = await getGoogleDriveClient(config);
|
||||
const name = input.data.path;
|
||||
|
||||
const existing = await findFileByName(client, name, config.folderId);
|
||||
const fullPath = input.data.path;
|
||||
const pathParts = fullPath.split("/").filter(Boolean);
|
||||
const fileName = pathParts.pop()!;
|
||||
const folderPath = pathParts.join("/");
|
||||
|
||||
const folderId = folderPath
|
||||
? 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"};
|
||||
|
||||
|
||||
await client.files.create({
|
||||
requestBody: {name, parents: [config.folderId]},
|
||||
requestBody: {name: fileName, parents: [folderId]},
|
||||
media: {body: Readable.from(input.data.file as Buffer)},
|
||||
fields: "id",
|
||||
supportsAllDrives: true,
|
||||
});
|
||||
|
||||
return {success: true, provider: 'google-drive'};
|
||||
if (input.data.url) {
|
||||
const url = await generateFileUrl(input);
|
||||
if (!url) {
|
||||
return {
|
||||
success: false,
|
||||
provider: "google-drive",
|
||||
response: "Unable to get url file"
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
provider: 'google-drive',
|
||||
url: url
|
||||
};
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
provider: 'google-drive',
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
export async function getGoogleDrive(
|
||||
config: GoogleDriveConfig,
|
||||
input: { data: StorageGetInput }
|
||||
input: { data: StorageGetInput, metadata: StorageMetaData },
|
||||
): Promise<StorageResult> {
|
||||
const client = await getGoogleDriveClient(config);
|
||||
const name = input.data.path;
|
||||
|
||||
const fileId = await findFileByName(client, name, config.folderId);
|
||||
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(
|
||||
@@ -156,6 +73,26 @@ export async function getGoogleDrive(
|
||||
{responseType: "arraybuffer"}
|
||||
);
|
||||
|
||||
|
||||
if (input.data.signedUrl) {
|
||||
const url = await generateFileUrl(input);
|
||||
|
||||
if (!url) {
|
||||
return {
|
||||
success: false,
|
||||
provider: "google-drive",
|
||||
response: "Unable to get url"
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
provider: "google-drive",
|
||||
file: Buffer.from(res.data as ArrayBuffer),
|
||||
url: url,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
provider: "google-drive",
|
||||
@@ -165,12 +102,10 @@ export async function getGoogleDrive(
|
||||
|
||||
export async function deleteGoogleDrive(
|
||||
config: GoogleDriveConfig,
|
||||
input: { data: StorageDeleteInput }
|
||||
input: { data: StorageDeleteInput, metadata?: StorageMetaData },
|
||||
): Promise<StorageResult> {
|
||||
const client = await getGoogleDriveClient(config);
|
||||
const name = input.data.path;
|
||||
|
||||
const fileId = await findFileByName(client, name, config.folderId);
|
||||
const fileId = await resolveFilePath(client, input.data.path, config.folderId);
|
||||
if (!fileId) return {success: false, provider: "google-drive", error: "File not found"};
|
||||
|
||||
await client.files.delete({fileId, supportsAllDrives: true});
|
||||
@@ -192,7 +127,7 @@ export async function pingGoogleDrive(config: GoogleDriveConfig): Promise<Storag
|
||||
});
|
||||
|
||||
await drive.files.get({fileId: file.data.id!, supportsAllDrives: true});
|
||||
// await drive.files.delete({fileId: file.data.id!, supportsAllDrives: true});
|
||||
await drive.files.delete({fileId: file.data.id!, supportsAllDrives: true});
|
||||
|
||||
return {success: true, provider: "google-drive", response: "Google Drive storage OK"};
|
||||
} catch (err: any) {
|
||||
|
||||
Reference in New Issue
Block a user