split func to misc
This commit is contained in:
parent
fa11b3fe7c
commit
0b26898c99
70
src/main.ts
70
src/main.ts
@ -1,5 +1,3 @@
|
|||||||
import * as path from "path";
|
|
||||||
import * as fs from "fs";
|
|
||||||
import { Buffer } from "buffer";
|
import { Buffer } from "buffer";
|
||||||
import { Readable } from "stream";
|
import { Readable } from "stream";
|
||||||
import * as mime from "mime-types";
|
import * as mime from "mime-types";
|
||||||
@ -24,6 +22,12 @@ import {
|
|||||||
DEFAULT_TBL_DELETE_HISTORY,
|
DEFAULT_TBL_DELETE_HISTORY,
|
||||||
} from "./localdb";
|
} from "./localdb";
|
||||||
|
|
||||||
|
import {
|
||||||
|
getFolderLevels,
|
||||||
|
bufferToArrayBuffer,
|
||||||
|
getObjectBodyToArrayBuffer,
|
||||||
|
} from "./misc";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
S3Client,
|
S3Client,
|
||||||
ListObjectsV2Command,
|
ListObjectsV2Command,
|
||||||
@ -47,68 +51,6 @@ const DEFAULT_SETTINGS: SaveRemotePluginSettings = {
|
|||||||
s3BucketName: "",
|
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<ArrayBuffer>
|
|
||||||
*/
|
|
||||||
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 {
|
export default class SaveRemotePlugin extends Plugin {
|
||||||
settings: SaveRemotePluginSettings;
|
settings: SaveRemotePluginSettings;
|
||||||
cm: CodeMirror.Editor;
|
cm: CodeMirror.Editor;
|
||||||
|
|||||||
66
src/misc.ts
Normal file
66
src/misc.ts
Normal file
@ -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<ArrayBuffer>
|
||||||
|
*/
|
||||||
|
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`);
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user