@sheetbase/drive
v1.1.0
Published
File management with Drive for Sheetbase backend app.
Downloads
8
Maintainers
Readme
Sheetbase Module: @sheetbase/drive
File management with Drive for Sheetbase backend app.
Install
Using npm: npm install --save @sheetbase/drive
import * as Drive from "@sheetbase/drive";
As a library: 1mbpy4unOm6RTKzU_awPJnt9mNncpFPXR9f3redN5YavB8PSYUDKe8Fo8
Set the Indentifier to DriveModule and select the lastest version, view code.
declare const DriveModule: { Drive: any };
const Drive = DriveModule.Drive;
Scopes
https://www.googleapis.com/auth/drive
Usage
Docs homepage: https://sheetbase.github.io/drive
API reference: https://sheetbase.github.io/drive/api
Getting started
Install: npm install --save @sheetbase/drive
Usage:
import { drive } from "@sheetbase/drive";
const Drive = drive({
uploadFolder: "1Abc..."
/* configs */
});
const file = Drive.uploadFile({
name: "file.txt",
base64Value: "data:text/plain;base64,Abc=="
});
Configs
Drive module configurations.
export interface Options extends Intergration {
// the upload folder id
uploadFolder: string;
// limits
allowTypes?: string[]; // mimetype list
maxSize?: number; // MB = 1,000,000 bytes
// structured by: <year>/<month>
nested?: boolean;
// customize the response url
urlBuilder?: string[] | { (id: string): string };
}
export interface Intergration {
AuthToken?: any;
}
uploadFolder
(required)
- Type:
string
- Default: REQUIRED
The Upload folder id.
urlBuilder
- Type:
string[]
orFunction
- Default:
['https://drive.google.com/uc?id=']
Customize the file url.
nested
- Type:
boolean
- Default:
undefined
Put upload file in Wordpress-like upload structure <year>/<month>
.
allowTypes
- Type:
string[]
- Default:
undefined
Use this if you want to accept certain types of file. Since the file is stored in Google Drive, there is no need to worry about script execution security. But uploader can upload any malicious file, these files may harm downloader devices.
maxSize
- Type:
number
- Default:
10
Upload file size limit in MB.
Drive
Interface for file management in Drive.
setIntegration
: Integrate with other servicesgetFileById
: Get a filegetFileInfoById
: Get a file informationuploadFile
: Upload a fileuploadFiles
: Upload multiple filesupdateFile
: Update a fileremoveFile
: Delete a file
Drive service detail:
class DriveService {
setIntegration<K extends keyof Intergration, Value>(
key: K,
value: Value
): DriveService;
registerRoutes(options: AddonRoutesOptions): void;
base64Parser(
base64Value: string
): {
mimeType: string;
size: number;
base64Body: string;
};
isFileAvailable(file: GoogleAppsScript.Drive.File): boolean;
isFileShared(file: GoogleAppsScript.Drive.File): boolean;
isValidFileType(mimeType: string): boolean;
isValidFileSize(sizeBytes: number): boolean;
getSharingPreset(preset: SharingPreset): SharingConfig;
generateFileName(fileName: string, rename?: RenamePolicy): string;
buildFileUrl(id: string): string;
getFileInfo(file: GoogleAppsScript.Drive.File): FileInfo;
getFilesInfo(files: GoogleAppsScript.Drive.File[]): FileInfo[];
getUploadFolder(): GoogleAppsScript.Drive.Folder;
getOrCreateFolderByName(
name: string,
parentFolder?: GoogleAppsScript.Drive.Folder
): GoogleAppsScript.Drive.Folder;
createFolderByYearAndMonth(
parentFolder?: GoogleAppsScript.Drive.Folder
): GoogleAppsScript.Drive.Folder;
createFileFromBase64Body(
parentFolder: GoogleAppsScript.Drive.Folder,
fileName: string,
mimeType: string,
base64Body: string
): GoogleAppsScript.Drive.File;
setFileSharing(
file: GoogleAppsScript.Drive.File,
sharing?: FileSharing
): GoogleAppsScript.Drive.File;
setEditPermissionForUser(
file: GoogleAppsScript.Drive.File,
auth: {
uid?: string;
email?: string;
}
): GoogleAppsScript.Drive.File;
hasViewPermission(file: GoogleAppsScript.Drive.File): boolean;
hasEditPermission(file: GoogleAppsScript.Drive.File): boolean;
getFileById(id: string): GoogleAppsScript.Drive.File;
getFileInfoById(id: string): FileInfo;
uploadFile(
fileData: UploadFile,
customFolder?: string,
renamePolicy?: RenamePolicy,
sharing?: FileSharing
): GoogleAppsScript.Drive.File;
uploadFiles(uploadResources: UploadResource[]): GoogleAppsScript.Drive.File[];
updateFile(id: string, data?: FileUpdateData): GoogleAppsScript.Drive.File;
removeFile(id: string): GoogleAppsScript.Drive.File;
}
Routes
To add routes to your app, see options AddonRoutesOptions:
Drive.registerRoutes(options?: AddonRoutesOptions);
Default disabled
Disabled routes by default, to enable set { disabledRoutes: [] }
in registerRoutes()
:
[
"put:/storage" // upload files
"post:/storage" // update a file
"delete:/storage" // delete a file
];
Endpoints
GET /storage
Get a file info. Route query:
id
: The file id
PUT /storage
Upload a file or multiple files. Route body:
file
: Single file upload data, (UploadFile
)folder
: Custom folder (for single file)rename
: Naming policy (for single file)share
: Sharing option (for single file)files
: Multiple files upload data, (UploadResource[]
)
Upload a file.
{
file: {
name: 'file.txt',
base64Value: '...'
}
}
Upload to a custom folder.
{
file: {/* ... */},
folder: 'My folder'
}
Rename upload file.
{
file: {/* ... */},
rename: 'AUTO', // AUTO | HASH
}
Custom file sharing.
{
file: {/* ... */},
share: 'PUBLIC' // PRIVATE (default) | PUBLIC | { access: '', permission: '' }
}
Upload multiple files.
{
files: [
{
file: {
name: "file.txt",
base64Value: "..."
}
},
{
file: {
name: "files.txt",
base64Value: "..."
}
}
];
}
POST /storage
Update a file information or sharing. Route body:
id
: The file iddata
: Update data
// Update data
{
name: '...', // change the file name
description: '...', // change the file description
content: '...', // change the file content
sharing: '...', // change the file sharing
}
DELETE /storage
Trash a file. Route body:
id
: The file id
License
@sheetbase/drive is released under the MIT license.