@gravityforms/gulp-tasks
Version:
Configurable Gulp tasks for use in Gravity Forms projects.
115 lines (114 loc) • 4.11 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.upload = void 0;
const fs = require("fs");
const path = require("path");
const utils = require("../utils/utilsMethods");
const mimeTypesByExt = {
/* eslint-disable key-spacing */
xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
xlsm: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
xls: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
txt: 'text/plain',
html: 'text/html',
htm: 'text/html',
csv: 'text/csv',
pdf: 'application/pdf',
docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
json: 'application/vnd.google-apps.script+json',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
png: 'image/png',
svg: 'image/svg+xml',
md5: 'text/plain',
pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
pptm: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
ppt: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
zip: 'application/zip',
/* eslint-enable key-spacing */
};
async function upload(params) {
let parentId;
let localPath;
let overwrite;
let update;
let sharedDriveId;
if (typeof params === 'string') {
parentId = 'root';
localPath = params;
overwrite = false;
update = false;
}
else {
parentId = await utils.resolveId(this, params.parentIdentifiers);
({ localPath, overwrite = false, update = false, sharedDriveId } = params);
}
const fileName = path.basename(localPath);
if (fs.lstatSync(localPath).isDirectory()) {
return await handleFolderUpload(this);
}
else {
return await handleFileUpload(this, params);
}
async function handleFolderUpload(utilsGDrive) {
const paramsMakeFolder = {
folderName: fileName,
parentIdentifiers: parentId,
overwrite,
};
const folderId = await utilsGDrive.makeFolder(paramsMakeFolder);
const children = fs.readdirSync(localPath);
const uploads = children.map((child) => {
const localPathChild = path.join(localPath, child);
return utilsGDrive.upload({
localPath: localPathChild,
parentIdentifiers: folderId,
overwrite,
});
});
await Promise.all(uploads);
return folderId;
}
async function handleFileUpload(utilsGDrive, params) {
const mimeType = mimeTypesByExt[path.extname(localPath).slice(1)];
const fileMetadata = {
name: fileName,
mimeType,
parents: [parentId],
};
if (update) {
const existingFileId = await utils.checkFileExistence(utilsGDrive, fileName, parentId, sharedDriveId);
if (existingFileId) {
const updateOptions = {
fileId: existingFileId,
media: {
mimeType: fileMetadata.mimeType,
body: fs.createReadStream(localPath),
},
fields: 'id',
};
if (sharedDriveId) {
updateOptions.supportsAllDrives = true;
}
const data = await utilsGDrive.call('files', 'update', updateOptions);
return data.id;
}
}
if (overwrite)
await utils.overwrite(utilsGDrive, fileMetadata);
const fileOptions = {
resource: fileMetadata,
media: {
mimeType: fileMetadata.mimeType,
body: fs.createReadStream(localPath),
},
fields: 'id',
};
if (sharedDriveId) {
fileOptions.supportsAllDrives = true;
}
const data = await utilsGDrive.call('files', 'create', fileOptions);
return data.id;
}
}
exports.upload = upload;
;