@nuasite/cms-core 0.64.3 → 0.65.1
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/asset-paths.d.ts.map +1 -0
- package/dist/asset-paths.js +55 -0
- package/dist/astro-source-analysis.d.ts.map +1 -0
- package/dist/astro-source-analysis.js +503 -0
- package/dist/byte-range.d.ts.map +1 -0
- package/dist/byte-range.js +31 -0
- package/dist/{types/check-entries.d.ts → check-entries.d.ts} +3 -3
- package/dist/check-entries.d.ts.map +1 -0
- package/dist/check-entries.js +98 -0
- package/dist/{types/check-live.d.ts → check-live.d.ts} +4 -4
- package/dist/check-live.d.ts.map +1 -0
- package/dist/check-live.js +49 -0
- package/dist/{types/check-shape.d.ts → check-shape.d.ts} +4 -4
- package/dist/check-shape.d.ts.map +1 -0
- package/dist/check-shape.js +119 -0
- package/dist/{types/check-write.d.ts → check-write.d.ts} +4 -4
- package/dist/check-write.d.ts.map +1 -0
- package/dist/check-write.js +273 -0
- package/dist/{types/check.d.ts → check.d.ts} +2 -2
- package/dist/check.d.ts.map +1 -0
- package/dist/check.js +366 -0
- package/dist/{types/collection-scanner.d.ts → collection-scanner.d.ts} +2 -2
- package/dist/collection-scanner.d.ts.map +1 -0
- package/dist/collection-scanner.js +1483 -0
- package/dist/{types/component-registry.d.ts → component-registry.d.ts} +1 -1
- package/dist/component-registry.d.ts.map +1 -0
- package/dist/component-registry.js +271 -0
- package/dist/{types/content-config-ast.d.ts → content-config-ast.d.ts} +1 -1
- package/dist/content-config-ast.d.ts.map +1 -0
- package/dist/content-config-ast.js +1614 -0
- package/dist/{types/core.d.ts → core.d.ts} +3 -3
- package/dist/core.d.ts.map +1 -0
- package/dist/core.js +94 -0
- package/dist/editor-write-model.d.ts.map +1 -0
- package/dist/editor-write-model.js +101 -0
- package/dist/fs/glob.d.ts.map +1 -0
- package/dist/fs/glob.js +79 -0
- package/dist/{types/fs → fs}/node-fs.d.ts +1 -1
- package/dist/fs/node-fs.d.ts.map +1 -0
- package/dist/fs/node-fs.js +199 -0
- package/dist/{types/fs → fs}/staged.d.ts +1 -1
- package/dist/fs/staged.d.ts.map +1 -0
- package/dist/fs/staged.js +203 -0
- package/dist/fs/types.d.ts.map +1 -0
- package/dist/fs/types.js +0 -0
- package/dist/{types/handlers → handlers}/entry-ops.d.ts +2 -2
- package/dist/handlers/entry-ops.d.ts.map +1 -0
- package/dist/handlers/entry-ops.js +846 -0
- package/dist/{types/handlers → handlers}/page-ops.d.ts +1 -1
- package/dist/handlers/page-ops.d.ts.map +1 -0
- package/dist/handlers/page-ops.js +165 -0
- package/dist/{types/handlers → handlers}/redirect-ops.d.ts +1 -1
- package/dist/handlers/redirect-ops.d.ts.map +1 -0
- package/dist/handlers/redirect-ops.js +111 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/media/contember.d.ts.map +1 -0
- package/dist/media/contember.js +79 -0
- package/dist/media/errors.d.ts.map +1 -0
- package/dist/media/errors.js +22 -0
- package/dist/{types/media → media}/file-system.d.ts +1 -1
- package/dist/media/file-system.d.ts.map +1 -0
- package/dist/media/file-system.js +124 -0
- package/dist/{types/media → media}/index.d.ts +8 -8
- package/dist/media/index.d.ts.map +1 -0
- package/dist/media/index.js +8 -0
- package/dist/media/local.d.ts.map +1 -0
- package/dist/media/local.js +209 -0
- package/dist/media/mime.d.ts.map +1 -0
- package/dist/media/mime.js +21 -0
- package/dist/media/names.d.ts.map +1 -0
- package/dist/media/names.js +23 -0
- package/dist/{types/media → media}/project-images.d.ts +1 -1
- package/dist/media/project-images.d.ts.map +1 -0
- package/dist/media/project-images.js +99 -0
- package/dist/media/s3.d.ts.map +1 -0
- package/dist/media/s3.js +160 -0
- package/dist/media/type-filter.d.ts.map +1 -0
- package/dist/media/type-filter.js +164 -0
- package/dist/{types/portable.d.ts → portable.d.ts} +25 -25
- package/dist/portable.d.ts.map +1 -0
- package/dist/portable.js +27 -0
- package/dist/{types/project-config-ast.d.ts → project-config-ast.d.ts} +1 -1
- package/dist/project-config-ast.d.ts.map +1 -0
- package/dist/project-config-ast.js +324 -0
- package/dist/public-static-file.d.ts.map +1 -0
- package/dist/public-static-file.js +40 -0
- package/dist/redirects.d.ts.map +1 -0
- package/dist/redirects.js +185 -0
- package/dist/route-scan.d.ts.map +1 -0
- package/dist/route-scan.js +23 -0
- package/dist/schema-port.d.ts.map +1 -0
- package/dist/schema-port.js +30 -0
- package/dist/{types/schema-probe.d.ts → schema-probe.d.ts} +2 -2
- package/dist/schema-probe.d.ts.map +1 -0
- package/dist/schema-probe.js +119 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +163 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +12 -12
- package/src/check-entries.ts +4 -4
- package/src/check-live.ts +4 -4
- package/src/check-shape.ts +5 -5
- package/src/check-write.ts +6 -6
- package/src/check.ts +15 -9
- package/src/collection-scanner.ts +3 -3
- package/src/component-registry.ts +1 -1
- package/src/content-config-ast.ts +1 -1
- package/src/core.ts +8 -8
- package/src/editor-write-model.ts +1 -1
- package/src/fs/node-fs.ts +2 -2
- package/src/fs/staged.ts +6 -3
- package/src/handlers/entry-ops.ts +7 -7
- package/src/handlers/page-ops.ts +2 -2
- package/src/handlers/redirect-ops.ts +2 -2
- package/src/index.ts +4 -4
- package/src/media/contember.ts +1 -1
- package/src/media/file-system.ts +4 -4
- package/src/media/index.ts +8 -8
- package/src/media/local.ts +3 -3
- package/src/media/project-images.ts +2 -2
- package/src/media/s3.ts +3 -3
- package/src/portable.ts +25 -25
- package/src/project-config-ast.ts +1 -1
- package/src/route-scan.ts +1 -1
- package/src/schema-probe.ts +2 -2
- package/src/tsconfig.json +5 -1
- package/dist/types/asset-paths.d.ts.map +0 -1
- package/dist/types/astro-source-analysis.d.ts.map +0 -1
- package/dist/types/byte-range.d.ts.map +0 -1
- package/dist/types/check-entries.d.ts.map +0 -1
- package/dist/types/check-live.d.ts.map +0 -1
- package/dist/types/check-shape.d.ts.map +0 -1
- package/dist/types/check-write.d.ts.map +0 -1
- package/dist/types/check.d.ts.map +0 -1
- package/dist/types/collection-scanner.d.ts.map +0 -1
- package/dist/types/component-registry.d.ts.map +0 -1
- package/dist/types/content-config-ast.d.ts.map +0 -1
- package/dist/types/core.d.ts.map +0 -1
- package/dist/types/editor-write-model.d.ts.map +0 -1
- package/dist/types/fs/glob.d.ts.map +0 -1
- package/dist/types/fs/node-fs.d.ts.map +0 -1
- package/dist/types/fs/staged.d.ts.map +0 -1
- package/dist/types/fs/types.d.ts.map +0 -1
- package/dist/types/handlers/entry-ops.d.ts.map +0 -1
- package/dist/types/handlers/page-ops.d.ts.map +0 -1
- package/dist/types/handlers/redirect-ops.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -5
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/media/contember.d.ts.map +0 -1
- package/dist/types/media/errors.d.ts.map +0 -1
- package/dist/types/media/file-system.d.ts.map +0 -1
- package/dist/types/media/index.d.ts.map +0 -1
- package/dist/types/media/local.d.ts.map +0 -1
- package/dist/types/media/mime.d.ts.map +0 -1
- package/dist/types/media/names.d.ts.map +0 -1
- package/dist/types/media/project-images.d.ts.map +0 -1
- package/dist/types/media/s3.d.ts.map +0 -1
- package/dist/types/media/type-filter.d.ts.map +0 -1
- package/dist/types/portable.d.ts.map +0 -1
- package/dist/types/project-config-ast.d.ts.map +0 -1
- package/dist/types/public-static-file.d.ts.map +0 -1
- package/dist/types/redirects.d.ts.map +0 -1
- package/dist/types/route-scan.d.ts.map +0 -1
- package/dist/types/schema-port.d.ts.map +0 -1
- package/dist/types/schema-probe.d.ts.map +0 -1
- package/dist/types/shared.d.ts.map +0 -1
- package/dist/types/tsconfig.tsbuildinfo +0 -1
- /package/dist/{types/asset-paths.d.ts → asset-paths.d.ts} +0 -0
- /package/dist/{types/astro-source-analysis.d.ts → astro-source-analysis.d.ts} +0 -0
- /package/dist/{types/byte-range.d.ts → byte-range.d.ts} +0 -0
- /package/dist/{types/editor-write-model.d.ts → editor-write-model.d.ts} +0 -0
- /package/dist/{types/fs → fs}/glob.d.ts +0 -0
- /package/dist/{types/fs → fs}/types.d.ts +0 -0
- /package/dist/{types/media → media}/contember.d.ts +0 -0
- /package/dist/{types/media → media}/errors.d.ts +0 -0
- /package/dist/{types/media → media}/local.d.ts +0 -0
- /package/dist/{types/media → media}/mime.d.ts +0 -0
- /package/dist/{types/media → media}/names.d.ts +0 -0
- /package/dist/{types/media → media}/s3.d.ts +0 -0
- /package/dist/{types/media → media}/type-filter.d.ts +0 -0
- /package/dist/{types/public-static-file.d.ts → public-static-file.d.ts} +0 -0
- /package/dist/{types/redirects.d.ts → redirects.d.ts} +0 -0
- /package/dist/{types/route-scan.d.ts → route-scan.d.ts} +0 -0
- /package/dist/{types/schema-port.d.ts → schema-port.d.ts} +0 -0
- /package/dist/{types/shared.d.ts → shared.d.ts} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CreatePageRequest, DeletePageRequest, DuplicatePageRequest, LayoutInfo, PageOperationResponse } from '@nuasite/cms-types';
|
|
2
|
-
import type { CmsFileSystem } from '../fs/types';
|
|
2
|
+
import type { CmsFileSystem } from '../fs/types.ts';
|
|
3
3
|
export interface PageOpsDeps {
|
|
4
4
|
fs: CmsFileSystem;
|
|
5
5
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page-ops.d.ts","sourceRoot":"","sources":["../../src/handlers/page-ops.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,UAAU,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAA;AACvI,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAKnD,MAAM,WAAW,WAAW;IAC3B,EAAE,EAAE,aAAa,CAAA;CACjB;AAMD,wBAAsB,UAAU,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAwB9G;AAED,wBAAsB,aAAa,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAqCpH;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,iBAAiB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAc9G;AAED,wBAAsB,UAAU,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAczE;AAED,wBAAsB,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAMtH"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { escapeHtml, slugify } from "../shared.js";
|
|
2
|
+
const PAGE_EXTENSIONS = ['.astro', '.md', '.mdx'];
|
|
3
|
+
function errorMessage(error) {
|
|
4
|
+
return error instanceof Error ? error.message : String(error);
|
|
5
|
+
}
|
|
6
|
+
export async function createPage(deps, request) {
|
|
7
|
+
const { title, slug } = request;
|
|
8
|
+
const normalizedSlug = slugify(slug || title);
|
|
9
|
+
if (!normalizedSlug) {
|
|
10
|
+
return { success: false, error: 'Could not generate a valid slug from the provided title/slug' };
|
|
11
|
+
}
|
|
12
|
+
const filePath = `src/pages/${normalizedSlug}.astro`;
|
|
13
|
+
if (await deps.fs.exists(filePath)) {
|
|
14
|
+
return { success: false, error: `Page already exists: ${filePath}` };
|
|
15
|
+
}
|
|
16
|
+
const layoutImport = await resolveLayoutImport(deps, request.layoutPath);
|
|
17
|
+
const content = generatePageContent(title, layoutImport);
|
|
18
|
+
try {
|
|
19
|
+
await deps.fs.writeFile(filePath, content);
|
|
20
|
+
const url = normalizedSlug === 'index' ? '/' : `/${normalizedSlug}`;
|
|
21
|
+
return { success: true, filePath, slug: normalizedSlug, url };
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
return { success: false, error: errorMessage(error) };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export async function duplicatePage(deps, request) {
|
|
28
|
+
const { sourcePagePath, slug, title } = request;
|
|
29
|
+
const normalizedSlug = slugify(slug);
|
|
30
|
+
if (!normalizedSlug) {
|
|
31
|
+
return { success: false, error: 'Could not generate a valid slug' };
|
|
32
|
+
}
|
|
33
|
+
const sourceFile = await findPageFile(deps, sourcePagePath);
|
|
34
|
+
if (!sourceFile) {
|
|
35
|
+
return { success: false, error: `Source page not found: ${sourcePagePath}` };
|
|
36
|
+
}
|
|
37
|
+
let content;
|
|
38
|
+
try {
|
|
39
|
+
content = await deps.fs.readFile(sourceFile);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return { success: false, error: `Could not read source file: ${sourceFile}` };
|
|
43
|
+
}
|
|
44
|
+
if (title) {
|
|
45
|
+
content = replacePageTitle(content, title);
|
|
46
|
+
}
|
|
47
|
+
const newFilePath = `src/pages/${normalizedSlug}.astro`;
|
|
48
|
+
if (await deps.fs.exists(newFilePath)) {
|
|
49
|
+
return { success: false, error: `Page already exists: ${newFilePath}` };
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
await deps.fs.writeFile(newFilePath, content);
|
|
53
|
+
const url = normalizedSlug === 'index' ? '/' : `/${normalizedSlug}`;
|
|
54
|
+
return { success: true, filePath: newFilePath, slug: normalizedSlug, url };
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
return { success: false, error: errorMessage(error) };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export async function deletePage(deps, request) {
|
|
61
|
+
const { pagePath } = request;
|
|
62
|
+
const pageFile = await findPageFile(deps, pagePath);
|
|
63
|
+
if (!pageFile) {
|
|
64
|
+
return { success: false, error: `Page not found: ${pagePath}` };
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
await deps.fs.remove(pageFile);
|
|
68
|
+
return { success: true, filePath: pageFile, url: pagePath };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
return { success: false, error: errorMessage(error) };
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export async function getLayouts(deps) {
|
|
75
|
+
const entries = await deps.fs.list('src/layouts');
|
|
76
|
+
const layouts = [];
|
|
77
|
+
for (const entry of entries) {
|
|
78
|
+
if (!entry.isDirectory && entry.name.endsWith('.astro')) {
|
|
79
|
+
layouts.push({
|
|
80
|
+
name: entry.name.slice(0, -'.astro'.length),
|
|
81
|
+
path: `src/layouts/${entry.name}`,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return layouts.sort((a, b) => a.name.localeCompare(b.name));
|
|
86
|
+
}
|
|
87
|
+
export async function checkSlugExists(deps, slug) {
|
|
88
|
+
const normalizedSlug = slugify(slug);
|
|
89
|
+
if (!normalizedSlug)
|
|
90
|
+
return { exists: false };
|
|
91
|
+
const found = await findPageFile(deps, `/${normalizedSlug}`);
|
|
92
|
+
return found ? { exists: true, filePath: found } : { exists: false };
|
|
93
|
+
}
|
|
94
|
+
// --- Internal helpers ---
|
|
95
|
+
async function findPageFile(deps, pagePath) {
|
|
96
|
+
const normalized = pagePath.replace(/^\//, '').replace(/\/$/, '') || 'index';
|
|
97
|
+
for (const ext of PAGE_EXTENSIONS) {
|
|
98
|
+
const direct = `src/pages/${normalized}${ext}`;
|
|
99
|
+
if (await deps.fs.exists(direct))
|
|
100
|
+
return direct;
|
|
101
|
+
}
|
|
102
|
+
for (const ext of PAGE_EXTENSIONS) {
|
|
103
|
+
const indexFile = `src/pages/${normalized}/index${ext}`;
|
|
104
|
+
if (await deps.fs.exists(indexFile))
|
|
105
|
+
return indexFile;
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
async function resolveLayoutImport(deps, layoutPath) {
|
|
110
|
+
if (layoutPath) {
|
|
111
|
+
const name = layoutPath.replace(/^.*\//, '').replace(/\.astro$/, '');
|
|
112
|
+
const importPath = `../${layoutPath.replace(/^src\//, '')}`;
|
|
113
|
+
return { importPath, componentName: pascalCase(name) };
|
|
114
|
+
}
|
|
115
|
+
const layouts = await getLayouts(deps);
|
|
116
|
+
if (layouts.length === 0)
|
|
117
|
+
return null;
|
|
118
|
+
const layout = layouts[0];
|
|
119
|
+
const importPath = `../${layout.path.replace(/^src\//, '')}`;
|
|
120
|
+
return { importPath, componentName: pascalCase(layout.name) };
|
|
121
|
+
}
|
|
122
|
+
function pascalCase(name) {
|
|
123
|
+
return name.replace(/(^|[-_])(\w)/g, (_, _sep, char) => char.toUpperCase());
|
|
124
|
+
}
|
|
125
|
+
function generatePageContent(title, layoutImport) {
|
|
126
|
+
const escapedTitle = title.replace(/'/g, "\\'").replace(/`/g, '\\`');
|
|
127
|
+
const htmlTitle = escapeHtml(title);
|
|
128
|
+
if (layoutImport) {
|
|
129
|
+
const { importPath, componentName } = layoutImport;
|
|
130
|
+
return `---
|
|
131
|
+
import ${componentName} from '${importPath}'
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
<${componentName} title="${escapedTitle}" description="">
|
|
135
|
+
\t<main>
|
|
136
|
+
\t\t<h1>${htmlTitle}</h1>
|
|
137
|
+
\t</main>
|
|
138
|
+
</${componentName}>
|
|
139
|
+
`;
|
|
140
|
+
}
|
|
141
|
+
return `---
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
<html lang="en">
|
|
146
|
+
\t<head>
|
|
147
|
+
\t\t<meta charset="utf-8" />
|
|
148
|
+
\t\t<meta name="viewport" content="width=device-width" />
|
|
149
|
+
\t\t<title>${escapedTitle}</title>
|
|
150
|
+
\t</head>
|
|
151
|
+
\t<body>
|
|
152
|
+
\t\t<main>
|
|
153
|
+
\t\t\t<h1>${htmlTitle}</h1>
|
|
154
|
+
\t\t</main>
|
|
155
|
+
\t</body>
|
|
156
|
+
</html>
|
|
157
|
+
`;
|
|
158
|
+
}
|
|
159
|
+
function replacePageTitle(content, newTitle) {
|
|
160
|
+
let result = content;
|
|
161
|
+
result = result.replace(/(title\s*=\s*")([^"]*)(")/, `$1${newTitle}$3`);
|
|
162
|
+
result = result.replace(/(<title>)([^<]*)(<\/title>)/, `$1${newTitle}$3`);
|
|
163
|
+
result = result.replace(/(<h1[^>]*>)([^<]*)(<\/h1>)/, `$1${escapeHtml(newTitle)}$3`);
|
|
164
|
+
return result;
|
|
165
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AddRedirectRequest, DeleteRedirectRequest, GetRedirectsResponse, RedirectOperationResponse, UpdateRedirectRequest } from '@nuasite/cms-types';
|
|
2
|
-
import type { CmsFileSystem } from '../fs/types';
|
|
2
|
+
import type { CmsFileSystem } from '../fs/types.ts';
|
|
3
3
|
export interface RedirectOpsDeps {
|
|
4
4
|
fs: CmsFileSystem;
|
|
5
5
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redirect-ops.d.ts","sourceRoot":"","sources":["../../src/handlers/redirect-ops.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,kBAAkB,EAClB,qBAAqB,EACrB,oBAAoB,EACpB,yBAAyB,EAEzB,qBAAqB,EACrB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAMnD,MAAM,WAAW,eAAe;IAC/B,EAAE,EAAE,aAAa,CAAA;CACjB;AAED,wBAAsB,aAAa,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAGxF;AAED,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAgBxH;AAED,wBAAsB,cAAc,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAyB9H;AAED,wBAAsB,cAAc,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,yBAAyB,CAAC,CAiB9H"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { CMS_REDIRECTS_FILE, parseRedirectRule } from "../redirects.js";
|
|
2
|
+
const DEFAULT_STATUS_CODE = 307;
|
|
3
|
+
const REDIRECTS_FILE = CMS_REDIRECTS_FILE;
|
|
4
|
+
export async function listRedirects(deps) {
|
|
5
|
+
const lines = await readRedirectsFile(deps);
|
|
6
|
+
return { rules: parseRedirectLines(lines) };
|
|
7
|
+
}
|
|
8
|
+
export async function addRedirect(deps, request) {
|
|
9
|
+
const { source, destination, statusCode = DEFAULT_STATUS_CODE } = request;
|
|
10
|
+
const invalid = ruleError(source, destination, statusCode);
|
|
11
|
+
if (invalid)
|
|
12
|
+
return { success: false, error: invalid };
|
|
13
|
+
const lines = await readRedirectsFile(deps);
|
|
14
|
+
const existing = parseRedirectLines(lines);
|
|
15
|
+
if (existing.some(r => r.source === source)) {
|
|
16
|
+
return { success: false, error: `Redirect already exists for ${source}` };
|
|
17
|
+
}
|
|
18
|
+
lines.push(formatRedirectLine(source, destination, statusCode));
|
|
19
|
+
await writeRedirectsFile(deps, lines);
|
|
20
|
+
return { success: true };
|
|
21
|
+
}
|
|
22
|
+
export async function updateRedirect(deps, request) {
|
|
23
|
+
const { lineIndex, source, destination, statusCode = DEFAULT_STATUS_CODE } = request;
|
|
24
|
+
// The same rules as adding: a line the editor list cannot show is one the editor cannot fix.
|
|
25
|
+
const invalid = ruleError(source, destination, statusCode);
|
|
26
|
+
if (invalid)
|
|
27
|
+
return { success: false, error: invalid };
|
|
28
|
+
const lines = await readRedirectsFile(deps);
|
|
29
|
+
const currentLine = lines[lineIndex]?.trim();
|
|
30
|
+
if (!currentLine || currentLine.startsWith('#')) {
|
|
31
|
+
return { success: false, error: 'Line at index is no longer a redirect rule — please refresh and try again' };
|
|
32
|
+
}
|
|
33
|
+
// The build keeps the first rule for a source, so moving a rule onto a source another rule already
|
|
34
|
+
// has would save and never apply. Editing a rule in place is always allowed, so a repeat already in
|
|
35
|
+
// the file can still be fixed.
|
|
36
|
+
const rules = parseRedirectLines(lines);
|
|
37
|
+
const current = rules.find(r => r.lineIndex === lineIndex);
|
|
38
|
+
if (current?.source !== source && rules.some(r => r.source === source && r.lineIndex !== lineIndex)) {
|
|
39
|
+
return { success: false, error: `Redirect already exists for ${source}` };
|
|
40
|
+
}
|
|
41
|
+
lines[lineIndex] = formatRedirectLine(source, destination, statusCode);
|
|
42
|
+
await writeRedirectsFile(deps, lines);
|
|
43
|
+
return { success: true };
|
|
44
|
+
}
|
|
45
|
+
export async function deleteRedirect(deps, request) {
|
|
46
|
+
const { lineIndex } = request;
|
|
47
|
+
const lines = await readRedirectsFile(deps);
|
|
48
|
+
if (lineIndex < 0 || lineIndex >= lines.length) {
|
|
49
|
+
return { success: false, error: `Invalid line index: ${lineIndex}` };
|
|
50
|
+
}
|
|
51
|
+
const line = lines[lineIndex].trim();
|
|
52
|
+
if (!line || line.startsWith('#')) {
|
|
53
|
+
return { success: false, error: 'Line is not a redirect rule' };
|
|
54
|
+
}
|
|
55
|
+
lines.splice(lineIndex, 1);
|
|
56
|
+
await writeRedirectsFile(deps, lines);
|
|
57
|
+
return { success: true };
|
|
58
|
+
}
|
|
59
|
+
// --- Internal helpers ---
|
|
60
|
+
function ruleError(source, destination, statusCode) {
|
|
61
|
+
if (!source || !destination)
|
|
62
|
+
return 'Source and destination are required';
|
|
63
|
+
if (/\s/.test(source) || /\s/.test(destination))
|
|
64
|
+
return 'Source and destination must not contain spaces';
|
|
65
|
+
if (!source.startsWith('/'))
|
|
66
|
+
return 'Source must start with /';
|
|
67
|
+
// Editors redirect; proxying (200) and 4xx stay with code, which the build enforces too.
|
|
68
|
+
if (![301, 302, 303, 307, 308].includes(statusCode))
|
|
69
|
+
return 'Status must be a redirect: 301, 302, 303, 307 or 308';
|
|
70
|
+
// Read back the way the build reads it, so the editor never saves a rule the build would not apply.
|
|
71
|
+
if (!parseRedirectRule(formatRedirectLine(source, destination, statusCode)))
|
|
72
|
+
return 'Destination must start with / or http:// or https://';
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
function formatRedirectLine(source, destination, statusCode) {
|
|
76
|
+
return statusCode === DEFAULT_STATUS_CODE
|
|
77
|
+
? `${source} ${destination}`
|
|
78
|
+
: `${source} ${destination} ${statusCode}`;
|
|
79
|
+
}
|
|
80
|
+
async function readRedirectsFile(deps) {
|
|
81
|
+
if (!(await deps.fs.exists(REDIRECTS_FILE)))
|
|
82
|
+
return [];
|
|
83
|
+
const content = await deps.fs.readFile(REDIRECTS_FILE);
|
|
84
|
+
return content.split('\n');
|
|
85
|
+
}
|
|
86
|
+
async function writeRedirectsFile(deps, lines) {
|
|
87
|
+
const trimmed = lines.slice();
|
|
88
|
+
while (trimmed.length > 0 && trimmed[trimmed.length - 1].trim() === '') {
|
|
89
|
+
trimmed.pop();
|
|
90
|
+
}
|
|
91
|
+
if (trimmed.length === 0) {
|
|
92
|
+
await deps.fs.writeFile(REDIRECTS_FILE, '');
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
await deps.fs.writeFile(REDIRECTS_FILE, trimmed.join('\n') + '\n');
|
|
96
|
+
}
|
|
97
|
+
function parseRedirectLines(lines) {
|
|
98
|
+
const rules = [];
|
|
99
|
+
for (let i = 0; i < lines.length; i++) {
|
|
100
|
+
const line = lines[i].trim();
|
|
101
|
+
if (!line || line.startsWith('#'))
|
|
102
|
+
continue;
|
|
103
|
+
// The same reading as the build's merge. The editor only manages rules whose source is a path.
|
|
104
|
+
const rule = parseRedirectRule(line);
|
|
105
|
+
if (!rule || !rule.source.startsWith('/'))
|
|
106
|
+
continue;
|
|
107
|
+
const statusCode = rule.status ? parseInt(rule.status, 10) : DEFAULT_STATUS_CODE;
|
|
108
|
+
rules.push({ source: rule.source, destination: rule.destination, statusCode, lineIndex: i });
|
|
109
|
+
}
|
|
110
|
+
return rules;
|
|
111
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createNodeFs } from './fs/node-fs.ts';
|
|
2
|
+
export { createLocalStorageAdapter, type LocalStorageOptions } from './media/local.ts';
|
|
3
|
+
export * from './portable.ts';
|
|
4
|
+
export { createPublicStaticFileChecker } from './public-static-file.ts';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9C,OAAO,EAAE,yBAAyB,EAAE,KAAK,mBAAmB,EAAE,MAAM,kBAAkB,CAAA;AACtF,cAAc,eAAe,CAAA;AAC7B,OAAO,EAAE,6BAA6B,EAAE,MAAM,yBAAyB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contember.d.ts","sourceRoot":"","sources":["../../src/media/contember.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAqB,MAAM,oBAAoB,CAAA;AAGhF,MAAM,WAAW,uBAAuB;IACvC,iEAAiE;IACjE,UAAU,EAAE,MAAM,CAAA;IAClB,wCAAwC;IACxC,WAAW,EAAE,MAAM,CAAA;IACnB,6EAA6E;IAC7E,YAAY,CAAC,EAAE,MAAM,CAAA;CACrB;AAED;;;;;;GAMG;AACH,wBAAgB,6BAA6B,CAAC,OAAO,EAAE,uBAAuB,GAAG,mBAAmB,CA8EnG"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { MEDIA_INVALID_ID } from "./errors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Media storage adapter that proxies to the Contember worker's CMS media endpoints.
|
|
4
|
+
* Uses the existing /cms/:projectSlug/media/* API backed by R2 storage + Contember database.
|
|
5
|
+
*
|
|
6
|
+
* All connection details (`apiBaseUrl`, `projectSlug`, `sessionToken`) are injected
|
|
7
|
+
* by the caller — the adapter never reads `process.env`.
|
|
8
|
+
*/
|
|
9
|
+
export function createContemberStorageAdapter(options) {
|
|
10
|
+
const { apiBaseUrl, projectSlug, sessionToken } = options;
|
|
11
|
+
const base = `${apiBaseUrl.replace(/\/$/, '')}/cms/${projectSlug}/media`;
|
|
12
|
+
function headers() {
|
|
13
|
+
const h = {};
|
|
14
|
+
if (sessionToken) {
|
|
15
|
+
h.Cookie = `NUA_SITE_SESSION_TOKEN=${sessionToken}`;
|
|
16
|
+
}
|
|
17
|
+
return h;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
async list(opts) {
|
|
21
|
+
const params = new URLSearchParams();
|
|
22
|
+
if (opts?.limit)
|
|
23
|
+
params.set('limit', String(opts.limit));
|
|
24
|
+
if (opts?.cursor)
|
|
25
|
+
params.set('cursor', opts.cursor);
|
|
26
|
+
const url = `${base}/list${params.toString() ? `?${params}` : ''}`;
|
|
27
|
+
const res = await fetch(url, {
|
|
28
|
+
method: 'GET',
|
|
29
|
+
headers: headers(),
|
|
30
|
+
credentials: 'include',
|
|
31
|
+
});
|
|
32
|
+
if (!res.ok) {
|
|
33
|
+
throw new Error(`Failed to list media (${res.status}): ${await res.text()}`);
|
|
34
|
+
}
|
|
35
|
+
const data = await res.json();
|
|
36
|
+
const payload = (data && typeof data === 'object') ? data : {};
|
|
37
|
+
return { items: [], folders: [], hasMore: false, ...payload };
|
|
38
|
+
},
|
|
39
|
+
async upload(file, filename, contentType) {
|
|
40
|
+
const blob = new Blob([new Uint8Array(file)], { type: contentType });
|
|
41
|
+
const formData = new FormData();
|
|
42
|
+
formData.append('file', blob, filename);
|
|
43
|
+
const res = await fetch(`${base}/upload`, {
|
|
44
|
+
method: 'POST',
|
|
45
|
+
headers: headers(),
|
|
46
|
+
body: formData,
|
|
47
|
+
credentials: 'include',
|
|
48
|
+
});
|
|
49
|
+
if (!res.ok) {
|
|
50
|
+
const text = await res.text().catch(() => '');
|
|
51
|
+
return { success: false, error: `Upload failed (${res.status}): ${text}` };
|
|
52
|
+
}
|
|
53
|
+
const data = await res.json();
|
|
54
|
+
const payload = (data && typeof data === 'object') ? data : {};
|
|
55
|
+
const result = { success: true, ...payload };
|
|
56
|
+
// Whatever the service reports stays marked as its own, like the status above, so it
|
|
57
|
+
// cannot read as one of the adapter's refusals.
|
|
58
|
+
if (result.error !== undefined)
|
|
59
|
+
result.error = `Upload failed: ${String(result.error)}`;
|
|
60
|
+
return result;
|
|
61
|
+
},
|
|
62
|
+
async delete(id) {
|
|
63
|
+
// `encodeURIComponent` keeps the id one path segment, but leaves `.` and `..` as they
|
|
64
|
+
// are, and a URL resolves those away: the request would address a different endpoint.
|
|
65
|
+
if (id === '' || id === '.' || id === '..')
|
|
66
|
+
return { success: false, error: MEDIA_INVALID_ID };
|
|
67
|
+
const res = await fetch(`${base}/${encodeURIComponent(id)}`, {
|
|
68
|
+
method: 'DELETE',
|
|
69
|
+
headers: headers(),
|
|
70
|
+
credentials: 'include',
|
|
71
|
+
});
|
|
72
|
+
if (!res.ok) {
|
|
73
|
+
const text = await res.text().catch(() => '');
|
|
74
|
+
return { success: false, error: `Delete failed (${res.status}): ${text}` };
|
|
75
|
+
}
|
|
76
|
+
return { success: true };
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/media/errors.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,qBAAqB,CAAA;AAClD,eAAO,MAAM,oBAAoB,wBAAwB,CAAA;AACzD,eAAO,MAAM,eAAe,yBAAyB,CAAA;AAErD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAI3E"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `error` a media adapter answers with when it refuses what the caller named, or
|
|
3
|
+
* when the file is not there. They are exact strings so a host can tell these apart
|
|
4
|
+
* from a storage failure by comparing, not by reading the text of an arbitrary error:
|
|
5
|
+
* an HTTP layer answers the first two with a 400 and the third with a 404.
|
|
6
|
+
*/
|
|
7
|
+
export const MEDIA_INVALID_ID = 'Invalid media id';
|
|
8
|
+
export const MEDIA_INVALID_FOLDER = 'Invalid folder name';
|
|
9
|
+
export const MEDIA_NOT_FOUND = 'Media file not found';
|
|
10
|
+
/**
|
|
11
|
+
* The HTTP status for a failed media adapter result: 400 for an id or folder the adapter
|
|
12
|
+
* refuses, 404 for a file that is not there, 500 for anything else. Only the exact
|
|
13
|
+
* strings above decide; the text of any other error may come from an SDK or an upstream
|
|
14
|
+
* service and is a storage failure whatever it says.
|
|
15
|
+
*/
|
|
16
|
+
export function mediaErrorStatus(error) {
|
|
17
|
+
if (error === MEDIA_INVALID_ID || error === MEDIA_INVALID_FOLDER)
|
|
18
|
+
return 400;
|
|
19
|
+
if (error === MEDIA_NOT_FOUND)
|
|
20
|
+
return 404;
|
|
21
|
+
return 500;
|
|
22
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { MediaStorageAdapter } from '@nuasite/cms-types';
|
|
2
|
-
import type { CmsFileSystem } from '../fs/types';
|
|
2
|
+
import type { CmsFileSystem } from '../fs/types.ts';
|
|
3
3
|
export interface FileSystemStorageOptions {
|
|
4
4
|
/** Directory to store media files in, relative to the filesystem's root. Default: `public/uploads`. */
|
|
5
5
|
dir?: string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-system.d.ts","sourceRoot":"","sources":["../../src/media/file-system.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA+C,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AAC1G,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAKnD,MAAM,WAAW,wBAAwB;IACxC,uGAAuG;IACvG,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,kEAAkE;IAClE,SAAS,CAAC,EAAE,MAAM,CAAA;CAClB;AAoBD;;;;GAIG;AACH,wBAAgB,8BAA8B,CAAC,EAAE,EAAE,aAAa,EAAE,OAAO,GAAE,wBAA6B,GAAG,mBAAmB,CAgG7H"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { MEDIA_INVALID_FOLDER, MEDIA_INVALID_ID, MEDIA_NOT_FOUND } from "./errors.js";
|
|
2
|
+
import { getFileExtension, mimeFromExt } from "./mime.js";
|
|
3
|
+
import { isUnsafeFolder } from "./names.js";
|
|
4
|
+
/**
|
|
5
|
+
* Written into a folder created empty, since a `CmsFileSystem` has directories only for the files
|
|
6
|
+
* in them. Hidden from listings like every name starting with a dot.
|
|
7
|
+
*/
|
|
8
|
+
const FOLDER_PLACEHOLDER = '.gitkeep';
|
|
9
|
+
/** `a/b` spelled the way ids and folders are handed out, or `null` for anything else. */
|
|
10
|
+
function parseNames(value) {
|
|
11
|
+
const names = value.split('/');
|
|
12
|
+
if (value.includes('\0') || names.some(name => name === '' || name === '.' || name === '..'))
|
|
13
|
+
return null;
|
|
14
|
+
return names;
|
|
15
|
+
}
|
|
16
|
+
function errorMessage(action, error) {
|
|
17
|
+
const code = error instanceof Error && 'code' in error && typeof error.code === 'string' ? error.code : undefined;
|
|
18
|
+
return code ? `Failed to ${action} (${code})` : `Failed to ${action}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Media stored in the project itself, through the same `CmsFileSystem` the core edits it with: the
|
|
22
|
+
* counterpart of the local adapter for a host without `node:fs`. Uploads land at
|
|
23
|
+
* `<dir>/<folder>/<uuid>.<ext>` and are listed newest first.
|
|
24
|
+
*/
|
|
25
|
+
export function createFileSystemStorageAdapter(fs, options = {}) {
|
|
26
|
+
const dir = (options.dir ?? 'public/uploads').split('/').filter(name => name !== '' && name !== '.').join('/');
|
|
27
|
+
const urlPrefix = (options.urlPrefix ?? '/uploads').replace(/\/$/, '');
|
|
28
|
+
/** The folder option as `a/b` (stray slashes dropped) plus its path under the root; `null` when it names no folder. */
|
|
29
|
+
function resolveFolder(input, use) {
|
|
30
|
+
const folder = (input ?? '').split('/').filter(Boolean).join('/');
|
|
31
|
+
if (!folder)
|
|
32
|
+
return { folder, path: dir };
|
|
33
|
+
if (parseNames(folder) === null || (use === 'write' && isUnsafeFolder(folder)))
|
|
34
|
+
return null;
|
|
35
|
+
return { folder, path: `${dir}/${folder}` };
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
staticFiles: { urlPrefix, dir },
|
|
39
|
+
async list(opts) {
|
|
40
|
+
const limit = opts?.limit ?? 50;
|
|
41
|
+
const offset = opts?.cursor ? parseInt(opts.cursor, 10) : 0;
|
|
42
|
+
const target = resolveFolder(opts?.folder, 'read');
|
|
43
|
+
if (!target)
|
|
44
|
+
return { items: [], folders: [], hasMore: false };
|
|
45
|
+
try {
|
|
46
|
+
const entries = (await fs.list(target.path)).filter(entry => !entry.name.startsWith('.'));
|
|
47
|
+
const folders = entries
|
|
48
|
+
.filter(entry => entry.isDirectory)
|
|
49
|
+
.map(entry => ({ name: entry.name, path: target.folder ? `${target.folder}/${entry.name}` : entry.name }))
|
|
50
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
51
|
+
const files = await Promise.all(entries
|
|
52
|
+
.filter(entry => !entry.isDirectory)
|
|
53
|
+
.map(async (entry) => ({ name: entry.name, stat: await fs.stat(`${target.path}/${entry.name}`) })));
|
|
54
|
+
files.sort((a, b) => b.stat.mtimeMs - a.stat.mtimeMs);
|
|
55
|
+
const urlFolder = target.folder ? `/${target.folder}` : '';
|
|
56
|
+
const items = files.slice(offset, offset + limit).map(file => {
|
|
57
|
+
const ext = getFileExtension(file.name);
|
|
58
|
+
return {
|
|
59
|
+
id: target.folder ? `${target.folder}/${file.name}` : file.name,
|
|
60
|
+
url: `${urlPrefix}${urlFolder}/${file.name}`,
|
|
61
|
+
filename: file.name,
|
|
62
|
+
contentType: mimeFromExt(ext ? `.${ext}` : ''),
|
|
63
|
+
uploadedAt: new Date(file.stat.mtimeMs).toISOString(),
|
|
64
|
+
folder: target.folder || undefined,
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
const hasMore = offset + limit < files.length;
|
|
68
|
+
return { items, folders, hasMore, cursor: hasMore ? String(offset + limit) : undefined };
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
// Deliberately without `cause`: whatever renders this error must not get the path back.
|
|
72
|
+
// oxlint-disable-next-line preserve-caught-error
|
|
73
|
+
throw new Error(errorMessage('list media', error));
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
async upload(file, filename, _contentType, uploadOpts) {
|
|
77
|
+
const target = resolveFolder(uploadOpts?.folder, 'write');
|
|
78
|
+
if (!target)
|
|
79
|
+
return { success: false, error: MEDIA_INVALID_FOLDER };
|
|
80
|
+
const ext = getFileExtension(filename);
|
|
81
|
+
const newFilename = `${crypto.randomUUID()}${ext ? `.${ext}` : ''}`;
|
|
82
|
+
try {
|
|
83
|
+
await fs.writeBytes(`${target.path}/${newFilename}`, file);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return { success: false, error: errorMessage('store media file', error) };
|
|
87
|
+
}
|
|
88
|
+
const urlFolder = target.folder ? `/${target.folder}` : '';
|
|
89
|
+
return {
|
|
90
|
+
success: true,
|
|
91
|
+
url: `${urlPrefix}${urlFolder}/${newFilename}`,
|
|
92
|
+
filename: newFilename,
|
|
93
|
+
id: target.folder ? `${target.folder}/${newFilename}` : newFilename,
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
async delete(id) {
|
|
97
|
+
if (parseNames(id) === null)
|
|
98
|
+
return { success: false, error: MEDIA_INVALID_ID };
|
|
99
|
+
const path = `${dir}/${id}`;
|
|
100
|
+
try {
|
|
101
|
+
if (!(await fs.exists(path)))
|
|
102
|
+
return { success: false, error: MEDIA_NOT_FOUND };
|
|
103
|
+
await fs.remove(path);
|
|
104
|
+
return { success: true };
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
return { success: false, error: errorMessage('delete media file', error) };
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
async createFolder(folder) {
|
|
111
|
+
const target = resolveFolder(folder, 'write');
|
|
112
|
+
if (!target)
|
|
113
|
+
return { success: false, error: MEDIA_INVALID_FOLDER };
|
|
114
|
+
try {
|
|
115
|
+
if (!(await fs.exists(target.path)))
|
|
116
|
+
await fs.writeFile(`${target.path}/${FOLDER_PLACEHOLDER}`, '');
|
|
117
|
+
return { success: true };
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
return { success: false, error: errorMessage('create folder', error) };
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { type ContemberStorageOptions, createContemberStorageAdapter } from './contember';
|
|
2
|
-
export { MEDIA_INVALID_FOLDER, MEDIA_INVALID_ID, MEDIA_NOT_FOUND, mediaErrorStatus } from './errors';
|
|
3
|
-
export { createFileSystemStorageAdapter, type FileSystemStorageOptions } from './file-system';
|
|
4
|
-
export { getFileExtension, MIME_BY_EXT, mimeFromExt } from './mime';
|
|
5
|
-
export { isUnsafeFolder } from './names';
|
|
6
|
-
export { listProjectImages, type ListProjectImagesOptions, uploadsDirRelativeToRoot } from './project-images';
|
|
7
|
-
export { createS3StorageAdapter, type S3StorageOptions } from './s3';
|
|
8
|
-
export { decodeFilteredMediaCursor, listFilteredMedia, MEDIA_CURSOR_TYPE_MISMATCH, MEDIA_INVALID_CURSOR, MediaCursorError, parseMediaTypeFilter, } from './type-filter';
|
|
1
|
+
export { type ContemberStorageOptions, createContemberStorageAdapter } from './contember.ts';
|
|
2
|
+
export { MEDIA_INVALID_FOLDER, MEDIA_INVALID_ID, MEDIA_NOT_FOUND, mediaErrorStatus } from './errors.ts';
|
|
3
|
+
export { createFileSystemStorageAdapter, type FileSystemStorageOptions } from './file-system.ts';
|
|
4
|
+
export { getFileExtension, MIME_BY_EXT, mimeFromExt } from './mime.ts';
|
|
5
|
+
export { isUnsafeFolder } from './names.ts';
|
|
6
|
+
export { listProjectImages, type ListProjectImagesOptions, uploadsDirRelativeToRoot } from './project-images.ts';
|
|
7
|
+
export { createS3StorageAdapter, type S3StorageOptions } from './s3.ts';
|
|
8
|
+
export { decodeFilteredMediaCursor, listFilteredMedia, MEDIA_CURSOR_TYPE_MISMATCH, MEDIA_INVALID_CURSOR, MediaCursorError, parseMediaTypeFilter, } from './type-filter.ts';
|
|
9
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/media/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,uBAAuB,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAA;AAC5F,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AACvG,OAAO,EAAE,8BAA8B,EAAE,KAAK,wBAAwB,EAAE,MAAM,kBAAkB,CAAA;AAChG,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AACtE,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAC3C,OAAO,EAAE,iBAAiB,EAAE,KAAK,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAA;AAChH,OAAO,EAAE,sBAAsB,EAAE,KAAK,gBAAgB,EAAE,MAAM,SAAS,CAAA;AACvE,OAAO,EACN,yBAAyB,EACzB,iBAAiB,EACjB,0BAA0B,EAC1B,oBAAoB,EACpB,gBAAgB,EAChB,oBAAoB,GACpB,MAAM,kBAAkB,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { createContemberStorageAdapter } from "./contember.js";
|
|
2
|
+
export { MEDIA_INVALID_FOLDER, MEDIA_INVALID_ID, MEDIA_NOT_FOUND, mediaErrorStatus } from "./errors.js";
|
|
3
|
+
export { createFileSystemStorageAdapter } from "./file-system.js";
|
|
4
|
+
export { getFileExtension, MIME_BY_EXT, mimeFromExt } from "./mime.js";
|
|
5
|
+
export { isUnsafeFolder } from "./names.js";
|
|
6
|
+
export { listProjectImages, uploadsDirRelativeToRoot } from "./project-images.js";
|
|
7
|
+
export { createS3StorageAdapter } from "./s3.js";
|
|
8
|
+
export { decodeFilteredMediaCursor, listFilteredMedia, MEDIA_CURSOR_TYPE_MISMATCH, MEDIA_INVALID_CURSOR, MediaCursorError, parseMediaTypeFilter, } from "./type-filter.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local.d.ts","sourceRoot":"","sources":["../../src/media/local.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoC,mBAAmB,EAAqB,MAAM,oBAAoB,CAAA;AAQlH,MAAM,WAAW,mBAAmB;IACnC;;;;;;;;OAQG;IACH,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,wDAAwD;IACxD,SAAS,CAAC,EAAE,MAAM,CAAA;CAClB;AAsCD,wBAAgB,yBAAyB,CAAC,OAAO,GAAE,mBAAwB,GAAG,mBAAmB,CA0KhG"}
|