@sprucelabs/spruce-file-utils 6.0.126

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,4 @@
1
+ Copyright (C) Spruce Labs, Inc - All Rights Reserved
2
+ Unauthorized copying of this repository, via any medium is strictly prohibited.
3
+ Proprietary and confidential
4
+ Written by Taylor Romero, et. el. <taylor@sprucelabs.ai>, September 2023
package/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # [Your Skill Name]
2
+
3
+ ## Useful links
4
+ * [Spruce Developer Documentation: https://developer.spruce.ai](https://developer.spruce.ai)
@@ -0,0 +1 @@
1
+ export { FileUploader, FileUploaderImpl } from './uploading/FileUploader';
@@ -0,0 +1,2 @@
1
+ export { FileUploaderImpl } from './uploading/FileUploader.js';
2
+ // export * from './types-module.js'
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,22 @@
1
+ export declare class FileUploaderImpl implements FileUploader {
2
+ static Class?: new () => FileUploader;
3
+ protected constructor();
4
+ static Uploader(): FileUploader;
5
+ upload(payload: UploadPayload): Promise<{
6
+ uri: string;
7
+ type: string;
8
+ }>;
9
+ private uploadFileToS3;
10
+ private findTypeAndBuffer;
11
+ }
12
+ interface UploadPayload {
13
+ base64Body: string;
14
+ name: string;
15
+ }
16
+ export interface FileUploader {
17
+ upload(payload: UploadPayload): Promise<{
18
+ uri: string;
19
+ type: string;
20
+ }>;
21
+ }
22
+ export {};
@@ -0,0 +1,79 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { assertOptions, SchemaError } from '@sprucelabs/schema';
11
+ import AWS from 'aws-sdk';
12
+ import FileType from 'file-type';
13
+ import MimeTypes from 'mime-types';
14
+ import * as uuid from 'uuid';
15
+ export class FileUploaderImpl {
16
+ constructor() { }
17
+ static Uploader() {
18
+ var _a;
19
+ return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)();
20
+ }
21
+ upload(payload) {
22
+ return __awaiter(this, void 0, void 0, function* () {
23
+ const { base64Body, name } = assertOptions(payload, ['base64Body', 'name']);
24
+ const { type, ext, buffer } = yield this.findTypeAndBuffer(base64Body, name);
25
+ const uniqueName = `${uuid.v4()}.${ext}`;
26
+ const { Location: uri } = yield this.uploadFileToS3(uniqueName, buffer, type);
27
+ return { uri, type };
28
+ });
29
+ }
30
+ uploadFileToS3(name, buffer, type) {
31
+ return __awaiter(this, void 0, void 0, function* () {
32
+ const { env: { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_S3_BUCKET }, } = assertOptions(process, [
33
+ 'env.AWS_ACCESS_KEY_ID',
34
+ 'env.AWS_SECRET_ACCESS_KEY',
35
+ 'env.AWS_S3_BUCKET',
36
+ ]);
37
+ const s3 = new AWS.S3({
38
+ accessKeyId: AWS_ACCESS_KEY_ID,
39
+ secretAccessKey: AWS_SECRET_ACCESS_KEY,
40
+ });
41
+ const s3Params = {
42
+ Bucket: AWS_S3_BUCKET,
43
+ Key: name,
44
+ Body: buffer,
45
+ ACL: 'public-read',
46
+ ContentType: type,
47
+ };
48
+ const uploadedFile = yield s3.upload(s3Params).promise();
49
+ return uploadedFile;
50
+ });
51
+ }
52
+ findTypeAndBuffer(base64Body, name) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const buffer = Buffer.from(base64Body, 'base64');
55
+ if (buffer.toString('base64') !== base64Body) {
56
+ throw new SchemaError({
57
+ code: 'INVALID_PARAMETERS',
58
+ parameters: ['base64Body'],
59
+ friendlyMessage: 'The supplied base64 data is invalid',
60
+ });
61
+ }
62
+ const res = yield FileType.fromBuffer(buffer);
63
+ if (res) {
64
+ return { type: res.mime, ext: res.ext, buffer };
65
+ }
66
+ else {
67
+ let ext;
68
+ let type = MimeTypes.lookup(name);
69
+ if (type) {
70
+ ext = MimeTypes.extension(type);
71
+ }
72
+ else {
73
+ type = 'application/octet-stream';
74
+ }
75
+ return { type, ext: ext ? ext : 'bin', buffer };
76
+ }
77
+ });
78
+ }
79
+ }
@@ -0,0 +1 @@
1
+ export { FileUploader, FileUploaderImpl } from './uploading/FileUploader';
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileUploaderImpl = void 0;
4
+ var FileUploader_1 = require("./uploading/FileUploader");
5
+ Object.defineProperty(exports, "FileUploaderImpl", { enumerable: true, get: function () { return FileUploader_1.FileUploaderImpl; } });
6
+ // export * from './types-module'
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
@@ -0,0 +1,22 @@
1
+ export declare class FileUploaderImpl implements FileUploader {
2
+ static Class?: new () => FileUploader;
3
+ protected constructor();
4
+ static Uploader(): FileUploader;
5
+ upload(payload: UploadPayload): Promise<{
6
+ uri: string;
7
+ type: string;
8
+ }>;
9
+ private uploadFileToS3;
10
+ private findTypeAndBuffer;
11
+ }
12
+ interface UploadPayload {
13
+ base64Body: string;
14
+ name: string;
15
+ }
16
+ export interface FileUploader {
17
+ upload(payload: UploadPayload): Promise<{
18
+ uri: string;
19
+ type: string;
20
+ }>;
21
+ }
22
+ export {};
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.FileUploaderImpl = void 0;
30
+ const schema_1 = require("@sprucelabs/schema");
31
+ const aws_sdk_1 = __importDefault(require("aws-sdk"));
32
+ const file_type_1 = __importDefault(require("file-type"));
33
+ const mime_types_1 = __importDefault(require("mime-types"));
34
+ const uuid = __importStar(require("uuid"));
35
+ class FileUploaderImpl {
36
+ constructor() { }
37
+ static Uploader() {
38
+ var _a;
39
+ return new ((_a = this.Class) !== null && _a !== void 0 ? _a : this)();
40
+ }
41
+ async upload(payload) {
42
+ const { base64Body, name } = (0, schema_1.assertOptions)(payload, ['base64Body', 'name']);
43
+ const { type, ext, buffer } = await this.findTypeAndBuffer(base64Body, name);
44
+ const uniqueName = `${uuid.v4()}.${ext}`;
45
+ const { Location: uri } = await this.uploadFileToS3(uniqueName, buffer, type);
46
+ return { uri, type };
47
+ }
48
+ async uploadFileToS3(name, buffer, type) {
49
+ const { env: { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_S3_BUCKET }, } = (0, schema_1.assertOptions)(process, [
50
+ 'env.AWS_ACCESS_KEY_ID',
51
+ 'env.AWS_SECRET_ACCESS_KEY',
52
+ 'env.AWS_S3_BUCKET',
53
+ ]);
54
+ const s3 = new aws_sdk_1.default.S3({
55
+ accessKeyId: AWS_ACCESS_KEY_ID,
56
+ secretAccessKey: AWS_SECRET_ACCESS_KEY,
57
+ });
58
+ const s3Params = {
59
+ Bucket: AWS_S3_BUCKET,
60
+ Key: name,
61
+ Body: buffer,
62
+ ACL: 'public-read',
63
+ ContentType: type,
64
+ };
65
+ const uploadedFile = await s3.upload(s3Params).promise();
66
+ return uploadedFile;
67
+ }
68
+ async findTypeAndBuffer(base64Body, name) {
69
+ const buffer = Buffer.from(base64Body, 'base64');
70
+ if (buffer.toString('base64') !== base64Body) {
71
+ throw new schema_1.SchemaError({
72
+ code: 'INVALID_PARAMETERS',
73
+ parameters: ['base64Body'],
74
+ friendlyMessage: 'The supplied base64 data is invalid',
75
+ });
76
+ }
77
+ const res = await file_type_1.default.fromBuffer(buffer);
78
+ if (res) {
79
+ return { type: res.mime, ext: res.ext, buffer };
80
+ }
81
+ else {
82
+ let ext;
83
+ let type = mime_types_1.default.lookup(name);
84
+ if (type) {
85
+ ext = mime_types_1.default.extension(type);
86
+ }
87
+ else {
88
+ type = 'application/octet-stream';
89
+ }
90
+ return { type, ext: ext ? ext : 'bin', buffer };
91
+ }
92
+ }
93
+ }
94
+ exports.FileUploaderImpl = FileUploaderImpl;
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@sprucelabs/spruce-file-utils",
3
+ "description": "Utils for working with files and Sprucebot.",
4
+ "version": "6.0.126",
5
+ "skill": {
6
+ "namespace": "files"
7
+ },
8
+ "homepage": "https://github.com/sprucelabsai/spruce-filess-skill",
9
+ "bugs": {
10
+ "url": "https://github.com/sprucelabsai/spruce-filess-skill/issues"
11
+ },
12
+ "main": "./build/index-module.js",
13
+ "types": "./build/index-module.d.ts",
14
+ "module": "./build/esm/index-module.js",
15
+ "sideEffects": false,
16
+ "files": [
17
+ "build/index-module.js",
18
+ "build/index-module.d.ts",
19
+ "build/types-module.js",
20
+ "build/types-module.d.ts",
21
+ "build/uploading/FileUploader.d.ts",
22
+ "build/uploading/FileUploader.js",
23
+ "build/esm/index-module.js",
24
+ "build/esm/index-module.d.ts",
25
+ "build/esm/types-module.js",
26
+ "build/esm/types-module.d.ts",
27
+ "build/esm/uploading/FileUploader.d.ts",
28
+ "build/esm/uploading/FileUploader.js"
29
+ ],
30
+ "keywords": [],
31
+ "scripts": {
32
+ "release": "npm publish"
33
+ },
34
+ "dependencies": {
35
+ "@sprucelabs/schema": "latest",
36
+ "aws-sdk": "^2.1558.0",
37
+ "file-type": "16.5.3",
38
+ "mime-types": "^2.1.35",
39
+ "uuid": "^9.0.1"
40
+ },
41
+ "engines": {
42
+ "yarn": "1.x"
43
+ }
44
+ }