import sharepoint from "../../sharepoint.app.mjs";
import fs from "fs";
import path from "path";
import { ConfigurationError } from "@pipedream/platform";
import constants from "../../common/constants.mjs";
export default {
key: "sharepoint-download-file",
name: "Download File",
description: "Download a Microsoft Sharepoint file to the /tmp directory. [See the documentation](https://learn.microsoft.com/en-us/graph/api/driveitem-get-content?view=graph-rest-1.0&tabs=http)",
version: "0.0.11",
annotations: {
destructiveHint: false,
openWorldHint: true,
readOnlyHint: true,
},
type: "action",
props: {
sharepoint,
siteId: {
propDefinition: [
sharepoint,
"siteId",
],
},
driveId: {
propDefinition: [
sharepoint,
"driveId",
(c) => ({
siteId: c.siteId,
}),
],
},
fileId: {
propDefinition: [
sharepoint,
"fileId",
(c) => ({
siteId: c.siteId,
driveId: c.driveId,
}),
],
},
filename: {
type: "string",
label: "Filename",
description: "The filename to save the downloaded file as in the `/tmp` directory",
},
convertToFormat: {
type: "string",
label: "Convert To Format",
description: "The format to convert the file to. See the [Format Options](https://learn.microsoft.com/en-us/graph/api/driveitem-get-content-format?view=graph-rest-1.0&tabs=http#format-options) for supported source formats",
options: [
"pdf",
"html",
],
optional: true,
},
syncDir: {
type: "dir",
accessMode: "write",
sync: true,
},
},
methods: {
async getOriginalFileData($) {
const { name: originalFilename } = await this.sharepoint.getDriveItem({
$,
driveId: this.driveId,
siteId: this.siteId,
fileId: this.fileId,
});
const originalExtension = path.extname(originalFilename).slice(1)
.toLowerCase() || undefined;
return {
originalFilename,
originalExtension,
};
},
formatNewFilename(originalExtension) {
const parsed = path.parse(this.filename);
if (this.convertToFormat) {
const base = parsed.ext
? parsed.name
: this.filename;
return `${base}.${this.convertToFormat.toLowerCase()}`;
}
if (parsed.ext) {
return this.filename;
}
return originalExtension
? `${this.filename}.${originalExtension}`
: this.filename;
},
validateConversionFormat(originalExtension) {
const supportedFormats = this.convertToFormat === "pdf"
? constants.PDF_CONVERTIBLE_FORMATS
: this.convertToFormat === "html"
? constants.HTML_CONVERTIBLE_FORMATS
: [];
if (!supportedFormats.includes(originalExtension)) {
throw new ConfigurationError(`The file extension "${originalExtension}" is not supported for conversion to "${this.convertToFormat}". Supported formats are: ${supportedFormats.join(", ")}`);
}
},
},
async run({ $ }) {
const {
originalFilename, originalExtension,
} = await this.getOriginalFileData($);
const filename = this.formatNewFilename(originalExtension);
if (this.convertToFormat) {
this.validateConversionFormat(originalExtension);
}
let response;
let args = {
$,
driveId: this.driveId,
fileId: this.fileId,
params: {
format: this.convertToFormat,
},
};
try {
response = await this.sharepoint.getFile({
...args,
responseType: "arraybuffer",
});
} catch {
await this.sharepoint.getFile(args);
}
const rawcontent = response.toString("base64");
const buffer = Buffer.from(rawcontent, "base64");
const downloadedFilepath = `${process.env.STASH_DIR || "/tmp"}/${filename}`;
fs.writeFileSync(downloadedFilepath, buffer);
const data = {
filename,
fileSize: `${buffer.length} bytes`,
extension: this.convertToFormat || originalExtension,
downloadedFilepath,
};
if (this.convertToFormat) {
data.originalFilename = originalFilename;
data.originalExtension = originalExtension;
}
return data;
},
};