From 0b26898c990e86cfa023c74760ec8a27023bd698 Mon Sep 17 00:00:00 2001 From: fyears Date: Sat, 23 Oct 2021 16:51:46 +0800 Subject: [PATCH] split func to misc --- src/main.ts | 70 +++++------------------------------------------------ src/misc.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 64 deletions(-) create mode 100644 src/misc.ts diff --git a/src/main.ts b/src/main.ts index 7524014..6379ee2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,3 @@ -import * as path from "path"; -import * as fs from "fs"; import { Buffer } from "buffer"; import { Readable } from "stream"; import * as mime from "mime-types"; @@ -24,6 +22,12 @@ import { DEFAULT_TBL_DELETE_HISTORY, } from "./localdb"; +import { + getFolderLevels, + bufferToArrayBuffer, + getObjectBodyToArrayBuffer, +} from "./misc"; + import { S3Client, ListObjectsV2Command, @@ -47,68 +51,6 @@ const DEFAULT_SETTINGS: SaveRemotePluginSettings = { s3BucketName: "", }; -const ignoreHiddenFiles = (item: string) => { - const basename = path.basename(item); - return basename === "." || basename[0] !== "."; -}; - -/** - * Util func for mkdir -p based on the "path" of original file or folder - * "a/b/c/" => ["a", "a/b", "a/b/c"] - * "a/b/c/d/e.txt" => ["a", "a/b", "a/b/c", "a/b/c/d"] - * @param x string - * @returns string[] might be empty - */ -const getFolderLevels = (x: string) => { - const res: string[] = []; - - if (x === "" || x === "/") { - return res; - } - - const y1 = x.split("/"); - let i = 0; - for (let index = 0; index + 1 < y1.length; index++) { - res.push(y1.slice(0, index + 1).join("/")); - } - return res; -}; - -/** - * https://stackoverflow.com/questions/8609289 - * @param b Buffer - * @returns ArrayBuffer - */ -const bufferToArrayBuffer = (b: Buffer) => { - return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength); -}; - -/** - * The Body of resp of aws GetObject has mix types - * and we want to get ArrayBuffer here. - * See https://github.com/aws/aws-sdk-js-v3/issues/1877 - * @param b The Body of GetObject - * @returns Promise - */ -const getObjectBodyToArrayBuffer = async ( - b: Readable | ReadableStream | Blob -) => { - if (b instanceof Readable) { - const chunks: Uint8Array[] = []; - for await (let chunk of b) { - chunks.push(chunk); - } - const buf = Buffer.concat(chunks); - return bufferToArrayBuffer(buf); - } else if (b instanceof ReadableStream) { - return await new Response(b, {}).arrayBuffer(); - } else if (b instanceof Blob) { - return await b.arrayBuffer(); - } else { - throw TypeError(`The type of ${b} is not one of the supported types`); - } -}; - export default class SaveRemotePlugin extends Plugin { settings: SaveRemotePluginSettings; cm: CodeMirror.Editor; diff --git a/src/misc.ts b/src/misc.ts new file mode 100644 index 0000000..f4215ca --- /dev/null +++ b/src/misc.ts @@ -0,0 +1,66 @@ +import * as path from "path"; +import * as fs from "fs"; +import { Buffer } from "buffer"; +import { Readable } from "stream"; + +export const ignoreHiddenFiles = (item: string) => { + const basename = path.basename(item); + return basename === "." || basename[0] !== "."; +}; + +/** + * Util func for mkdir -p based on the "path" of original file or folder + * "a/b/c/" => ["a", "a/b", "a/b/c"] + * "a/b/c/d/e.txt" => ["a", "a/b", "a/b/c", "a/b/c/d"] + * @param x string + * @returns string[] might be empty + */ +export const getFolderLevels = (x: string) => { + const res: string[] = []; + + if (x === "" || x === "/") { + return res; + } + + const y1 = x.split("/"); + let i = 0; + for (let index = 0; index + 1 < y1.length; index++) { + res.push(y1.slice(0, index + 1).join("/")); + } + return res; +}; + +/** + * https://stackoverflow.com/questions/8609289 + * @param b Buffer + * @returns ArrayBuffer + */ +export const bufferToArrayBuffer = (b: Buffer) => { + return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength); +}; + +/** + * The Body of resp of aws GetObject has mix types + * and we want to get ArrayBuffer here. + * See https://github.com/aws/aws-sdk-js-v3/issues/1877 + * @param b The Body of GetObject + * @returns Promise + */ +export const getObjectBodyToArrayBuffer = async ( + b: Readable | ReadableStream | Blob +) => { + if (b instanceof Readable) { + const chunks: Uint8Array[] = []; + for await (let chunk of b) { + chunks.push(chunk); + } + const buf = Buffer.concat(chunks); + return bufferToArrayBuffer(buf); + } else if (b instanceof ReadableStream) { + return await new Response(b, {}).arrayBuffer(); + } else if (b instanceof Blob) { + return await b.arrayBuffer(); + } else { + throw TypeError(`The type of ${b} is not one of the supported types`); + } +};