@stacksjs/storage 0.70.87 → 0.70.90
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/dist/adapters/bun.js +226 -0
- package/dist/adapters/index.js +5 -0
- package/dist/adapters/local.js +225 -0
- package/dist/adapters/memory.js +318 -0
- package/dist/adapters/s3.js +471 -0
- package/dist/adapters/scoped.js +142 -0
- package/dist/copy.js +30 -0
- package/dist/delete.js +103 -0
- package/dist/drivers/aws.js +94 -0
- package/dist/drivers/bun.js +88 -0
- package/dist/drivers/index.js +4 -0
- package/dist/drivers/local.js +88 -0
- package/dist/drivers/memory.js +67 -0
- package/dist/facade.js +226 -0
- package/dist/files.js +126 -0
- package/dist/folders.js +36 -0
- package/dist/fs.js +7 -0
- package/dist/glob.js +40 -0
- package/dist/hash.js +33 -0
- package/dist/helpers.js +28 -0
- package/dist/image.js +8 -22
- package/dist/index.js +25 -2808
- package/dist/mime-verify.js +47 -0
- package/dist/move.js +55 -0
- package/dist/path-sanitize.js +84 -0
- package/dist/put-file.js +85 -0
- package/dist/s3-presigned-post.js +68 -0
- package/dist/signed-url.js +86 -0
- package/dist/static-serve.js +110 -0
- package/dist/storage.js +9 -0
- package/dist/types/filesystem.js +40 -0
- package/dist/types.js +25 -0
- package/dist/uploaded-file.js +114 -0
- package/dist/visibility.js +3 -0
- package/dist/zip.js +41 -0
- package/package.json +6 -6
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { basename, extname, join } from "node:path";
|
|
2
|
+
import { Storage } from "./facade";
|
|
3
|
+
|
|
4
|
+
export class UploadedFile {
|
|
5
|
+
_file;
|
|
6
|
+
_hashName = null;
|
|
7
|
+
constructor(file) {
|
|
8
|
+
this._file = file;
|
|
9
|
+
}
|
|
10
|
+
get name() {
|
|
11
|
+
return this._file.name;
|
|
12
|
+
}
|
|
13
|
+
get extension() {
|
|
14
|
+
return extname(this._file.name).slice(1).toLowerCase();
|
|
15
|
+
}
|
|
16
|
+
get mimeType() {
|
|
17
|
+
return this._file.type;
|
|
18
|
+
}
|
|
19
|
+
get size() {
|
|
20
|
+
return this._file.size;
|
|
21
|
+
}
|
|
22
|
+
get file() {
|
|
23
|
+
return this._file;
|
|
24
|
+
}
|
|
25
|
+
hashName(extension) {
|
|
26
|
+
if (!this._hashName) {
|
|
27
|
+
const hash = crypto.randomUUID().replace(/-/g, "").slice(0, 32), ext = extension || this.extension;
|
|
28
|
+
this._hashName = ext ? `${hash}.${ext}` : hash;
|
|
29
|
+
}
|
|
30
|
+
return this._hashName;
|
|
31
|
+
}
|
|
32
|
+
async arrayBuffer() {
|
|
33
|
+
return this._file.arrayBuffer();
|
|
34
|
+
}
|
|
35
|
+
async bytes() {
|
|
36
|
+
const buffer = await this._file.arrayBuffer();
|
|
37
|
+
return new Uint8Array(buffer);
|
|
38
|
+
}
|
|
39
|
+
async text() {
|
|
40
|
+
return this._file.text();
|
|
41
|
+
}
|
|
42
|
+
isValid() {
|
|
43
|
+
return this._file.size > 0;
|
|
44
|
+
}
|
|
45
|
+
async store(path = "", disk = "local") {
|
|
46
|
+
const filename = this.hashName(), fullPath = path ? join(path, filename) : filename, contents = await this.bytes();
|
|
47
|
+
await Storage.disk(disk).write(fullPath, contents);
|
|
48
|
+
return fullPath;
|
|
49
|
+
}
|
|
50
|
+
async storeAs(path, name, disk = "local") {
|
|
51
|
+
if (name.includes("..") || name.includes("/") || name.includes("\\"))
|
|
52
|
+
throw Error("Invalid filename: path traversal or directory separator detected");
|
|
53
|
+
const sanitizedName = basename(name), fullPath = path ? join(path, sanitizedName) : sanitizedName, contents = await this.bytes();
|
|
54
|
+
await Storage.disk(disk).write(fullPath, contents);
|
|
55
|
+
return fullPath;
|
|
56
|
+
}
|
|
57
|
+
async storePublicly(path = "") {
|
|
58
|
+
return this.store(path, "public");
|
|
59
|
+
}
|
|
60
|
+
async storePubliclyAs(path, name) {
|
|
61
|
+
return this.storeAs(path, name, "public");
|
|
62
|
+
}
|
|
63
|
+
async move(path, disk = "local") {
|
|
64
|
+
const contents = await this.bytes();
|
|
65
|
+
await Storage.disk(disk).write(path, contents);
|
|
66
|
+
return path;
|
|
67
|
+
}
|
|
68
|
+
getClientOriginalName() {
|
|
69
|
+
return basename(this._file.name, extname(this._file.name));
|
|
70
|
+
}
|
|
71
|
+
getClientOriginalExtension() {
|
|
72
|
+
return this.extension;
|
|
73
|
+
}
|
|
74
|
+
getClientMimeType() {
|
|
75
|
+
return this._file.type;
|
|
76
|
+
}
|
|
77
|
+
getSize() {
|
|
78
|
+
const bytes = this._file.size;
|
|
79
|
+
if (bytes < 1024)
|
|
80
|
+
return `${bytes} B`;
|
|
81
|
+
if (bytes < 1048576)
|
|
82
|
+
return `${(bytes / 1024).toFixed(2)} KB`;
|
|
83
|
+
if (bytes < 1073741824)
|
|
84
|
+
return `${(bytes / 1048576).toFixed(2)} MB`;
|
|
85
|
+
return `${(bytes / 1073741824).toFixed(2)} GB`;
|
|
86
|
+
}
|
|
87
|
+
isOneOf(mimeTypes) {
|
|
88
|
+
return mimeTypes.some((type) => {
|
|
89
|
+
if (type.endsWith("/*")) {
|
|
90
|
+
const category = type.slice(0, -2);
|
|
91
|
+
return this._file.type.startsWith(category);
|
|
92
|
+
}
|
|
93
|
+
return this._file.type === type;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
isImage() {
|
|
97
|
+
return this._file.type.startsWith("image/");
|
|
98
|
+
}
|
|
99
|
+
isVideo() {
|
|
100
|
+
return this._file.type.startsWith("video/");
|
|
101
|
+
}
|
|
102
|
+
isAudio() {
|
|
103
|
+
return this._file.type.startsWith("audio/");
|
|
104
|
+
}
|
|
105
|
+
isPdf() {
|
|
106
|
+
return this._file.type === "application/pdf";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
export function uploadedFile(file) {
|
|
110
|
+
return new UploadedFile(file);
|
|
111
|
+
}
|
|
112
|
+
export function uploadedFiles(files) {
|
|
113
|
+
return files.map((f) => new UploadedFile(f));
|
|
114
|
+
}
|
package/dist/zip.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { runCommand } from "@stacksjs/cli";
|
|
2
|
+
function shellEscape(_arg) {
|
|
3
|
+
return `'${_arg.replace(/'/g, "'\\''")}'`;
|
|
4
|
+
}
|
|
5
|
+
export async function zip(from, to, options) {
|
|
6
|
+
const toPath = to || "archive.zip";
|
|
7
|
+
if (Array.isArray(from)) {
|
|
8
|
+
const fromPath = from.map((f) => shellEscape(f)).join(" ");
|
|
9
|
+
return runCommand(`zip -r ${shellEscape(toPath)} ${fromPath}`, options);
|
|
10
|
+
}
|
|
11
|
+
return runCommand(`zip -r ${shellEscape(toPath)} ${shellEscape(from)}`, options);
|
|
12
|
+
}
|
|
13
|
+
export async function unzip(paths) {
|
|
14
|
+
if (Array.isArray(paths))
|
|
15
|
+
return runCommand(`unzip ${paths.map((p) => shellEscape(p)).join(" ")}`);
|
|
16
|
+
return runCommand(`unzip ${shellEscape(paths)}`);
|
|
17
|
+
}
|
|
18
|
+
export function archive(paths) {
|
|
19
|
+
return zip(paths);
|
|
20
|
+
}
|
|
21
|
+
export function unarchive(paths) {
|
|
22
|
+
return unzip(paths);
|
|
23
|
+
}
|
|
24
|
+
export function compress(paths) {
|
|
25
|
+
return zip(paths);
|
|
26
|
+
}
|
|
27
|
+
export function decompress(paths) {
|
|
28
|
+
return unzip(paths);
|
|
29
|
+
}
|
|
30
|
+
export function gzipSync(data, options) {
|
|
31
|
+
return Bun.gzipSync(data, options);
|
|
32
|
+
}
|
|
33
|
+
export function gunzipSync(data) {
|
|
34
|
+
return Bun.gunzipSync(data);
|
|
35
|
+
}
|
|
36
|
+
export function deflateSync(data, options) {
|
|
37
|
+
return Bun.deflateSync(data, options);
|
|
38
|
+
}
|
|
39
|
+
export function inflateSync(data) {
|
|
40
|
+
return Bun.inflateSync(data);
|
|
41
|
+
}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/storage",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.70.
|
|
5
|
+
"version": "0.70.90",
|
|
6
6
|
"description": "The Stacks file system.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -63,12 +63,12 @@
|
|
|
63
63
|
}
|
|
64
64
|
},
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
"@stacksjs/arrays": "0.70.
|
|
66
|
+
"@stacksjs/arrays": "0.70.90",
|
|
67
67
|
"better-dx": "^0.2.16",
|
|
68
|
-
"@stacksjs/error-handling": "0.70.
|
|
69
|
-
"@stacksjs/path": "0.70.
|
|
70
|
-
"@stacksjs/strings": "0.70.
|
|
71
|
-
"@stacksjs/types": "0.70.
|
|
68
|
+
"@stacksjs/error-handling": "0.70.90",
|
|
69
|
+
"@stacksjs/path": "0.70.90",
|
|
70
|
+
"@stacksjs/strings": "0.70.90",
|
|
71
|
+
"@stacksjs/types": "0.70.90",
|
|
72
72
|
"sharp": "^0.35.3"
|
|
73
73
|
}
|
|
74
74
|
}
|