@crowi/plugin-storage-local 0.1.0-alpha.0
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 +21 -0
- package/dist/index.d.mts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +111 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +75 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 Sotaro KARASAWA <sotaro.k@gmail.com>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod/v3';
|
|
2
|
+
import { StorageDriver, CrowiPlugin } from '@crowi/plugin-api';
|
|
3
|
+
|
|
4
|
+
declare const LocalStorageConfigSchema: z.ZodObject<{
|
|
5
|
+
/** Relative paths resolve against the directory containing `crowi.config.json`. */
|
|
6
|
+
rootDir: z.ZodDefault<z.ZodString>;
|
|
7
|
+
}, "strict", z.ZodTypeAny, {
|
|
8
|
+
rootDir: string;
|
|
9
|
+
}, {
|
|
10
|
+
rootDir?: string | undefined;
|
|
11
|
+
}>;
|
|
12
|
+
type LocalStorageConfig = z.infer<typeof LocalStorageConfigSchema>;
|
|
13
|
+
declare const plugin: CrowiPlugin;
|
|
14
|
+
|
|
15
|
+
declare function createLocalDriver(config: LocalStorageConfig): StorageDriver;
|
|
16
|
+
/**
|
|
17
|
+
* Lightweight stat helper — useful for tests / debugging. Returns
|
|
18
|
+
* null when the key does not exist.
|
|
19
|
+
*/
|
|
20
|
+
declare function statKey(driverConfig: LocalStorageConfig, key: string): Promise<{
|
|
21
|
+
size: number;
|
|
22
|
+
} | null>;
|
|
23
|
+
|
|
24
|
+
export { createLocalDriver, plugin as default, statKey };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod/v3';
|
|
2
|
+
import { StorageDriver, CrowiPlugin } from '@crowi/plugin-api';
|
|
3
|
+
|
|
4
|
+
declare const LocalStorageConfigSchema: z.ZodObject<{
|
|
5
|
+
/** Relative paths resolve against the directory containing `crowi.config.json`. */
|
|
6
|
+
rootDir: z.ZodDefault<z.ZodString>;
|
|
7
|
+
}, "strict", z.ZodTypeAny, {
|
|
8
|
+
rootDir: string;
|
|
9
|
+
}, {
|
|
10
|
+
rootDir?: string | undefined;
|
|
11
|
+
}>;
|
|
12
|
+
type LocalStorageConfig = z.infer<typeof LocalStorageConfigSchema>;
|
|
13
|
+
declare const plugin: CrowiPlugin;
|
|
14
|
+
|
|
15
|
+
declare function createLocalDriver(config: LocalStorageConfig): StorageDriver;
|
|
16
|
+
/**
|
|
17
|
+
* Lightweight stat helper — useful for tests / debugging. Returns
|
|
18
|
+
* null when the key does not exist.
|
|
19
|
+
*/
|
|
20
|
+
declare function statKey(driverConfig: LocalStorageConfig, key: string): Promise<{
|
|
21
|
+
size: number;
|
|
22
|
+
} | null>;
|
|
23
|
+
|
|
24
|
+
export { createLocalDriver, plugin as default, statKey };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
createLocalDriver: () => createLocalDriver,
|
|
34
|
+
default: () => index_default,
|
|
35
|
+
statKey: () => statKey
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
var import_node_fs = require("fs");
|
|
39
|
+
var import_promises = require("fs/promises");
|
|
40
|
+
var import_node_path = __toESM(require("path"));
|
|
41
|
+
var import_node_stream = require("stream");
|
|
42
|
+
var import_promises2 = require("stream/promises");
|
|
43
|
+
var import_v3 = require("zod/v3");
|
|
44
|
+
var LocalStorageConfigSchema = import_v3.z.object({
|
|
45
|
+
/** Relative paths resolve against the directory containing `crowi.config.json`. */
|
|
46
|
+
rootDir: import_v3.z.string().default("data/uploads")
|
|
47
|
+
}).strict();
|
|
48
|
+
var plugin = {
|
|
49
|
+
name: "@crowi/plugin-storage-local",
|
|
50
|
+
version: "0.1.0-dev",
|
|
51
|
+
configSchema: LocalStorageConfigSchema,
|
|
52
|
+
adminPlacement: {
|
|
53
|
+
label: "\u30ED\u30FC\u30AB\u30EB\u30B9\u30C8\u30EC\u30FC\u30B8",
|
|
54
|
+
icon: "hard-drive"
|
|
55
|
+
// section omitted: derived from registerStorage → 'storage'
|
|
56
|
+
},
|
|
57
|
+
registerStorage: (registry, ctx) => {
|
|
58
|
+
const driver = createLocalDriver(ctx.config());
|
|
59
|
+
registry.register("local", driver);
|
|
60
|
+
ctx.log.debug("registered local storage driver");
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var index_default = plugin;
|
|
64
|
+
function createLocalDriver(config) {
|
|
65
|
+
const root = import_node_path.default.isAbsolute(config.rootDir) ? config.rootDir : import_node_path.default.resolve(process.cwd(), config.rootDir);
|
|
66
|
+
const resolveSafe = (key) => {
|
|
67
|
+
const resolved = import_node_path.default.resolve(root, key);
|
|
68
|
+
if (resolved !== root && !resolved.startsWith(root + import_node_path.default.sep)) {
|
|
69
|
+
throw new Error(`Storage key '${key}' resolves outside of rootDir`);
|
|
70
|
+
}
|
|
71
|
+
return resolved;
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
async put(key, body, _meta) {
|
|
75
|
+
const target = resolveSafe(key);
|
|
76
|
+
await (0, import_promises.mkdir)(import_node_path.default.dirname(target), { recursive: true });
|
|
77
|
+
const source = body instanceof Buffer ? import_node_stream.Readable.from(body) : body;
|
|
78
|
+
await (0, import_promises2.pipeline)(source, (0, import_node_fs.createWriteStream)(target));
|
|
79
|
+
return { key };
|
|
80
|
+
},
|
|
81
|
+
async get(key) {
|
|
82
|
+
const target = resolveSafe(key);
|
|
83
|
+
if (!(0, import_node_fs.existsSync)(target)) {
|
|
84
|
+
throw Object.assign(new Error(`Storage key '${key}' does not exist`), { code: "ENOENT" });
|
|
85
|
+
}
|
|
86
|
+
return (0, import_node_fs.createReadStream)(target);
|
|
87
|
+
},
|
|
88
|
+
async delete(key) {
|
|
89
|
+
const target = resolveSafe(key);
|
|
90
|
+
await (0, import_promises.rm)(target, { force: true });
|
|
91
|
+
}
|
|
92
|
+
// No signedUrl: local files are streamed via `get()` through the
|
|
93
|
+
// API. Browsers cannot fetch them directly anyway.
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async function statKey(driverConfig, key) {
|
|
97
|
+
const root = import_node_path.default.isAbsolute(driverConfig.rootDir) ? driverConfig.rootDir : import_node_path.default.resolve(process.cwd(), driverConfig.rootDir);
|
|
98
|
+
const target = import_node_path.default.resolve(root, key);
|
|
99
|
+
try {
|
|
100
|
+
const s = await (0, import_promises.stat)(target);
|
|
101
|
+
return { size: s.size };
|
|
102
|
+
} catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
107
|
+
0 && (module.exports = {
|
|
108
|
+
createLocalDriver,
|
|
109
|
+
statKey
|
|
110
|
+
});
|
|
111
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createReadStream, createWriteStream, existsSync } from 'node:fs';\nimport { mkdir, rm, stat } from 'node:fs/promises';\nimport path from 'node:path';\nimport { Readable } from 'node:stream';\nimport { pipeline } from 'node:stream/promises';\nimport { z } from 'zod/v3';\nimport type { CrowiPlugin, StorageDriver } from '@crowi/plugin-api';\n\nconst LocalStorageConfigSchema = z\n .object({\n /** Relative paths resolve against the directory containing `crowi.config.json`. */\n rootDir: z.string().default('data/uploads'),\n })\n .strict();\n\ntype LocalStorageConfig = z.infer<typeof LocalStorageConfigSchema>;\n\nconst plugin: CrowiPlugin = {\n name: '@crowi/plugin-storage-local',\n version: '0.1.0-dev',\n configSchema: LocalStorageConfigSchema,\n adminPlacement: {\n label: 'ローカルストレージ',\n icon: 'hard-drive',\n // section omitted: derived from registerStorage → 'storage'\n },\n\n registerStorage: (registry, ctx) => {\n const driver: StorageDriver = createLocalDriver(ctx.config<LocalStorageConfig>());\n registry.register('local', driver);\n ctx.log.debug('registered local storage driver');\n },\n};\n\nexport default plugin;\n\nexport function createLocalDriver(config: LocalStorageConfig): StorageDriver {\n const root = path.isAbsolute(config.rootDir) ? config.rootDir : path.resolve(process.cwd(), config.rootDir);\n\n // Reject `..` / absolute paths that would escape rootDir\n // (e.g. key = '../../etc/passwd').\n const resolveSafe = (key: string): string => {\n const resolved = path.resolve(root, key);\n if (resolved !== root && !resolved.startsWith(root + path.sep)) {\n throw new Error(`Storage key '${key}' resolves outside of rootDir`);\n }\n return resolved;\n };\n\n return {\n async put(key, body, _meta) {\n const target = resolveSafe(key);\n await mkdir(path.dirname(target), { recursive: true });\n const source = body instanceof Buffer ? Readable.from(body) : body;\n await pipeline(source, createWriteStream(target));\n return { key };\n },\n\n async get(key) {\n const target = resolveSafe(key);\n if (!existsSync(target)) {\n // Object.assign (not a cast) so tsup emits valid JS for ts-node;\n // the cast form leaves a raw `err.code = …` that ts-node rejects.\n throw Object.assign(new Error(`Storage key '${key}' does not exist`), { code: 'ENOENT' });\n }\n return createReadStream(target);\n },\n\n async delete(key) {\n const target = resolveSafe(key);\n await rm(target, { force: true });\n },\n\n // No signedUrl: local files are streamed via `get()` through the\n // API. Browsers cannot fetch them directly anyway.\n };\n}\n\n/**\n * Lightweight stat helper — useful for tests / debugging. Returns\n * null when the key does not exist.\n */\nexport async function statKey(driverConfig: LocalStorageConfig, key: string): Promise<{ size: number } | null> {\n const root = path.isAbsolute(driverConfig.rootDir) ? driverConfig.rootDir : path.resolve(process.cwd(), driverConfig.rootDir);\n const target = path.resolve(root, key);\n try {\n const s = await stat(target);\n return { size: s.size };\n } catch {\n return null;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAAgE;AAChE,sBAAgC;AAChC,uBAAiB;AACjB,yBAAyB;AACzB,IAAAA,mBAAyB;AACzB,gBAAkB;AAGlB,IAAM,2BAA2B,YAC9B,OAAO;AAAA;AAAA,EAEN,SAAS,YAAE,OAAO,EAAE,QAAQ,cAAc;AAC5C,CAAC,EACA,OAAO;AAIV,IAAM,SAAsB;AAAA,EAC1B,MAAM;AAAA,EACN,SAAS;AAAA,EACT,cAAc;AAAA,EACd,gBAAgB;AAAA,IACd,OAAO;AAAA,IACP,MAAM;AAAA;AAAA,EAER;AAAA,EAEA,iBAAiB,CAAC,UAAU,QAAQ;AAClC,UAAM,SAAwB,kBAAkB,IAAI,OAA2B,CAAC;AAChF,aAAS,SAAS,SAAS,MAAM;AACjC,QAAI,IAAI,MAAM,iCAAiC;AAAA,EACjD;AACF;AAEA,IAAO,gBAAQ;AAER,SAAS,kBAAkB,QAA2C;AAC3E,QAAM,OAAO,iBAAAC,QAAK,WAAW,OAAO,OAAO,IAAI,OAAO,UAAU,iBAAAA,QAAK,QAAQ,QAAQ,IAAI,GAAG,OAAO,OAAO;AAI1G,QAAM,cAAc,CAAC,QAAwB;AAC3C,UAAM,WAAW,iBAAAA,QAAK,QAAQ,MAAM,GAAG;AACvC,QAAI,aAAa,QAAQ,CAAC,SAAS,WAAW,OAAO,iBAAAA,QAAK,GAAG,GAAG;AAC9D,YAAM,IAAI,MAAM,gBAAgB,GAAG,+BAA+B;AAAA,IACpE;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,MAAM,IAAI,KAAK,MAAM,OAAO;AAC1B,YAAM,SAAS,YAAY,GAAG;AAC9B,gBAAM,uBAAM,iBAAAA,QAAK,QAAQ,MAAM,GAAG,EAAE,WAAW,KAAK,CAAC;AACrD,YAAM,SAAS,gBAAgB,SAAS,4BAAS,KAAK,IAAI,IAAI;AAC9D,gBAAM,2BAAS,YAAQ,kCAAkB,MAAM,CAAC;AAChD,aAAO,EAAE,IAAI;AAAA,IACf;AAAA,IAEA,MAAM,IAAI,KAAK;AACb,YAAM,SAAS,YAAY,GAAG;AAC9B,UAAI,KAAC,2BAAW,MAAM,GAAG;AAGvB,cAAM,OAAO,OAAO,IAAI,MAAM,gBAAgB,GAAG,kBAAkB,GAAG,EAAE,MAAM,SAAS,CAAC;AAAA,MAC1F;AACA,iBAAO,iCAAiB,MAAM;AAAA,IAChC;AAAA,IAEA,MAAM,OAAO,KAAK;AAChB,YAAM,SAAS,YAAY,GAAG;AAC9B,gBAAM,oBAAG,QAAQ,EAAE,OAAO,KAAK,CAAC;AAAA,IAClC;AAAA;AAAA;AAAA,EAIF;AACF;AAMA,eAAsB,QAAQ,cAAkC,KAA+C;AAC7G,QAAM,OAAO,iBAAAA,QAAK,WAAW,aAAa,OAAO,IAAI,aAAa,UAAU,iBAAAA,QAAK,QAAQ,QAAQ,IAAI,GAAG,aAAa,OAAO;AAC5H,QAAM,SAAS,iBAAAA,QAAK,QAAQ,MAAM,GAAG;AACrC,MAAI;AACF,UAAM,IAAI,UAAM,sBAAK,MAAM;AAC3B,WAAO,EAAE,MAAM,EAAE,KAAK;AAAA,EACxB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":["import_promises","path"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createReadStream, createWriteStream, existsSync } from "fs";
|
|
3
|
+
import { mkdir, rm, stat } from "fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { Readable } from "stream";
|
|
6
|
+
import { pipeline } from "stream/promises";
|
|
7
|
+
import { z } from "zod/v3";
|
|
8
|
+
var LocalStorageConfigSchema = z.object({
|
|
9
|
+
/** Relative paths resolve against the directory containing `crowi.config.json`. */
|
|
10
|
+
rootDir: z.string().default("data/uploads")
|
|
11
|
+
}).strict();
|
|
12
|
+
var plugin = {
|
|
13
|
+
name: "@crowi/plugin-storage-local",
|
|
14
|
+
version: "0.1.0-dev",
|
|
15
|
+
configSchema: LocalStorageConfigSchema,
|
|
16
|
+
adminPlacement: {
|
|
17
|
+
label: "\u30ED\u30FC\u30AB\u30EB\u30B9\u30C8\u30EC\u30FC\u30B8",
|
|
18
|
+
icon: "hard-drive"
|
|
19
|
+
// section omitted: derived from registerStorage → 'storage'
|
|
20
|
+
},
|
|
21
|
+
registerStorage: (registry, ctx) => {
|
|
22
|
+
const driver = createLocalDriver(ctx.config());
|
|
23
|
+
registry.register("local", driver);
|
|
24
|
+
ctx.log.debug("registered local storage driver");
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
var index_default = plugin;
|
|
28
|
+
function createLocalDriver(config) {
|
|
29
|
+
const root = path.isAbsolute(config.rootDir) ? config.rootDir : path.resolve(process.cwd(), config.rootDir);
|
|
30
|
+
const resolveSafe = (key) => {
|
|
31
|
+
const resolved = path.resolve(root, key);
|
|
32
|
+
if (resolved !== root && !resolved.startsWith(root + path.sep)) {
|
|
33
|
+
throw new Error(`Storage key '${key}' resolves outside of rootDir`);
|
|
34
|
+
}
|
|
35
|
+
return resolved;
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
async put(key, body, _meta) {
|
|
39
|
+
const target = resolveSafe(key);
|
|
40
|
+
await mkdir(path.dirname(target), { recursive: true });
|
|
41
|
+
const source = body instanceof Buffer ? Readable.from(body) : body;
|
|
42
|
+
await pipeline(source, createWriteStream(target));
|
|
43
|
+
return { key };
|
|
44
|
+
},
|
|
45
|
+
async get(key) {
|
|
46
|
+
const target = resolveSafe(key);
|
|
47
|
+
if (!existsSync(target)) {
|
|
48
|
+
throw Object.assign(new Error(`Storage key '${key}' does not exist`), { code: "ENOENT" });
|
|
49
|
+
}
|
|
50
|
+
return createReadStream(target);
|
|
51
|
+
},
|
|
52
|
+
async delete(key) {
|
|
53
|
+
const target = resolveSafe(key);
|
|
54
|
+
await rm(target, { force: true });
|
|
55
|
+
}
|
|
56
|
+
// No signedUrl: local files are streamed via `get()` through the
|
|
57
|
+
// API. Browsers cannot fetch them directly anyway.
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
async function statKey(driverConfig, key) {
|
|
61
|
+
const root = path.isAbsolute(driverConfig.rootDir) ? driverConfig.rootDir : path.resolve(process.cwd(), driverConfig.rootDir);
|
|
62
|
+
const target = path.resolve(root, key);
|
|
63
|
+
try {
|
|
64
|
+
const s = await stat(target);
|
|
65
|
+
return { size: s.size };
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export {
|
|
71
|
+
createLocalDriver,
|
|
72
|
+
index_default as default,
|
|
73
|
+
statKey
|
|
74
|
+
};
|
|
75
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { createReadStream, createWriteStream, existsSync } from 'node:fs';\nimport { mkdir, rm, stat } from 'node:fs/promises';\nimport path from 'node:path';\nimport { Readable } from 'node:stream';\nimport { pipeline } from 'node:stream/promises';\nimport { z } from 'zod/v3';\nimport type { CrowiPlugin, StorageDriver } from '@crowi/plugin-api';\n\nconst LocalStorageConfigSchema = z\n .object({\n /** Relative paths resolve against the directory containing `crowi.config.json`. */\n rootDir: z.string().default('data/uploads'),\n })\n .strict();\n\ntype LocalStorageConfig = z.infer<typeof LocalStorageConfigSchema>;\n\nconst plugin: CrowiPlugin = {\n name: '@crowi/plugin-storage-local',\n version: '0.1.0-dev',\n configSchema: LocalStorageConfigSchema,\n adminPlacement: {\n label: 'ローカルストレージ',\n icon: 'hard-drive',\n // section omitted: derived from registerStorage → 'storage'\n },\n\n registerStorage: (registry, ctx) => {\n const driver: StorageDriver = createLocalDriver(ctx.config<LocalStorageConfig>());\n registry.register('local', driver);\n ctx.log.debug('registered local storage driver');\n },\n};\n\nexport default plugin;\n\nexport function createLocalDriver(config: LocalStorageConfig): StorageDriver {\n const root = path.isAbsolute(config.rootDir) ? config.rootDir : path.resolve(process.cwd(), config.rootDir);\n\n // Reject `..` / absolute paths that would escape rootDir\n // (e.g. key = '../../etc/passwd').\n const resolveSafe = (key: string): string => {\n const resolved = path.resolve(root, key);\n if (resolved !== root && !resolved.startsWith(root + path.sep)) {\n throw new Error(`Storage key '${key}' resolves outside of rootDir`);\n }\n return resolved;\n };\n\n return {\n async put(key, body, _meta) {\n const target = resolveSafe(key);\n await mkdir(path.dirname(target), { recursive: true });\n const source = body instanceof Buffer ? Readable.from(body) : body;\n await pipeline(source, createWriteStream(target));\n return { key };\n },\n\n async get(key) {\n const target = resolveSafe(key);\n if (!existsSync(target)) {\n // Object.assign (not a cast) so tsup emits valid JS for ts-node;\n // the cast form leaves a raw `err.code = …` that ts-node rejects.\n throw Object.assign(new Error(`Storage key '${key}' does not exist`), { code: 'ENOENT' });\n }\n return createReadStream(target);\n },\n\n async delete(key) {\n const target = resolveSafe(key);\n await rm(target, { force: true });\n },\n\n // No signedUrl: local files are streamed via `get()` through the\n // API. Browsers cannot fetch them directly anyway.\n };\n}\n\n/**\n * Lightweight stat helper — useful for tests / debugging. Returns\n * null when the key does not exist.\n */\nexport async function statKey(driverConfig: LocalStorageConfig, key: string): Promise<{ size: number } | null> {\n const root = path.isAbsolute(driverConfig.rootDir) ? driverConfig.rootDir : path.resolve(process.cwd(), driverConfig.rootDir);\n const target = path.resolve(root, key);\n try {\n const s = await stat(target);\n return { size: s.size };\n } catch {\n return null;\n }\n}\n"],"mappings":";AAAA,SAAS,kBAAkB,mBAAmB,kBAAkB;AAChE,SAAS,OAAO,IAAI,YAAY;AAChC,OAAO,UAAU;AACjB,SAAS,gBAAgB;AACzB,SAAS,gBAAgB;AACzB,SAAS,SAAS;AAGlB,IAAM,2BAA2B,EAC9B,OAAO;AAAA;AAAA,EAEN,SAAS,EAAE,OAAO,EAAE,QAAQ,cAAc;AAC5C,CAAC,EACA,OAAO;AAIV,IAAM,SAAsB;AAAA,EAC1B,MAAM;AAAA,EACN,SAAS;AAAA,EACT,cAAc;AAAA,EACd,gBAAgB;AAAA,IACd,OAAO;AAAA,IACP,MAAM;AAAA;AAAA,EAER;AAAA,EAEA,iBAAiB,CAAC,UAAU,QAAQ;AAClC,UAAM,SAAwB,kBAAkB,IAAI,OAA2B,CAAC;AAChF,aAAS,SAAS,SAAS,MAAM;AACjC,QAAI,IAAI,MAAM,iCAAiC;AAAA,EACjD;AACF;AAEA,IAAO,gBAAQ;AAER,SAAS,kBAAkB,QAA2C;AAC3E,QAAM,OAAO,KAAK,WAAW,OAAO,OAAO,IAAI,OAAO,UAAU,KAAK,QAAQ,QAAQ,IAAI,GAAG,OAAO,OAAO;AAI1G,QAAM,cAAc,CAAC,QAAwB;AAC3C,UAAM,WAAW,KAAK,QAAQ,MAAM,GAAG;AACvC,QAAI,aAAa,QAAQ,CAAC,SAAS,WAAW,OAAO,KAAK,GAAG,GAAG;AAC9D,YAAM,IAAI,MAAM,gBAAgB,GAAG,+BAA+B;AAAA,IACpE;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,MAAM,IAAI,KAAK,MAAM,OAAO;AAC1B,YAAM,SAAS,YAAY,GAAG;AAC9B,YAAM,MAAM,KAAK,QAAQ,MAAM,GAAG,EAAE,WAAW,KAAK,CAAC;AACrD,YAAM,SAAS,gBAAgB,SAAS,SAAS,KAAK,IAAI,IAAI;AAC9D,YAAM,SAAS,QAAQ,kBAAkB,MAAM,CAAC;AAChD,aAAO,EAAE,IAAI;AAAA,IACf;AAAA,IAEA,MAAM,IAAI,KAAK;AACb,YAAM,SAAS,YAAY,GAAG;AAC9B,UAAI,CAAC,WAAW,MAAM,GAAG;AAGvB,cAAM,OAAO,OAAO,IAAI,MAAM,gBAAgB,GAAG,kBAAkB,GAAG,EAAE,MAAM,SAAS,CAAC;AAAA,MAC1F;AACA,aAAO,iBAAiB,MAAM;AAAA,IAChC;AAAA,IAEA,MAAM,OAAO,KAAK;AAChB,YAAM,SAAS,YAAY,GAAG;AAC9B,YAAM,GAAG,QAAQ,EAAE,OAAO,KAAK,CAAC;AAAA,IAClC;AAAA;AAAA;AAAA,EAIF;AACF;AAMA,eAAsB,QAAQ,cAAkC,KAA+C;AAC7G,QAAM,OAAO,KAAK,WAAW,aAAa,OAAO,IAAI,aAAa,UAAU,KAAK,QAAQ,QAAQ,IAAI,GAAG,aAAa,OAAO;AAC5H,QAAM,SAAS,KAAK,QAAQ,MAAM,GAAG;AACrC,MAAI;AACF,UAAM,IAAI,MAAM,KAAK,MAAM;AAC3B,WAAO,EAAE,MAAM,EAAE,KAAK;AAAA,EACxB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@crowi/plugin-storage-local",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "Local filesystem storage driver for Crowi 2.0. Default-on; preserves the v1.x data/uploads/ layout.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"zod": "^4.4.3"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^24",
|
|
27
|
+
"tsup": "^8.3.5",
|
|
28
|
+
"typescript": "^5.8.3",
|
|
29
|
+
"zod": "^4.4.3",
|
|
30
|
+
"@crowi/tsconfig": "0.1.0-alpha.0",
|
|
31
|
+
"@crowi/plugin-api": "0.1.0-alpha.0"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@crowi/plugin-api": "^0.1.0-alpha.0"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsup",
|
|
38
|
+
"dev": "tsup --watch --no-clean",
|
|
39
|
+
"type-check": "tsc --noEmit"
|
|
40
|
+
}
|
|
41
|
+
}
|