forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystem.js
More file actions
187 lines (164 loc) · 4.46 KB
/
filesystem.js
File metadata and controls
187 lines (164 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import fs from 'fs-extra';
import path from 'path';
import crypto from 'crypto';
import { getDataRoot, urlToPath, isContainer } from '../utils/url.js';
// Note: Data directory is ensured in server.js after DATA_ROOT is set
/**
* Check if resource exists
* @param {string} urlPath
* @returns {Promise<boolean>}
*/
export async function exists(urlPath) {
const filePath = urlToPath(urlPath);
return fs.pathExists(filePath);
}
/**
* Get resource stats
* @param {string} urlPath
* @returns {Promise<{isDirectory: boolean, size: number, mtime: Date, etag: string} | null>}
*/
export async function stat(urlPath) {
const filePath = urlToPath(urlPath);
try {
const stats = await fs.stat(filePath);
return {
isDirectory: stats.isDirectory(),
size: stats.size,
mtime: stats.mtime,
etag: `"${crypto.createHash('md5').update(stats.mtime.toISOString() + stats.size).digest('hex')}"`
};
} catch {
return null;
}
}
/**
* Read resource content
* @param {string} urlPath
* @returns {Promise<Buffer | null>}
*/
export async function read(urlPath) {
const filePath = urlToPath(urlPath);
try {
return await fs.readFile(filePath);
} catch {
return null;
}
}
/**
* Create a readable stream for a resource (supports range requests)
* @param {string} urlPath
* @param {object} options - { start, end } byte range options
* @returns {{ stream: ReadStream, filePath: string } | null}
*/
export function createReadStream(urlPath, options = {}) {
const filePath = urlToPath(urlPath);
// Check file exists before creating stream (createReadStream doesn't throw sync)
if (!fs.pathExistsSync(filePath)) {
return null;
}
try {
const stream = fs.createReadStream(filePath, options);
return { stream, filePath };
} catch {
return null;
}
}
/**
* Write resource content
* @param {string} urlPath
* @param {Buffer | string} content
* @returns {Promise<boolean>}
*/
export async function write(urlPath, content) {
const filePath = urlToPath(urlPath);
try {
// Ensure parent directory exists
await fs.ensureDir(path.dirname(filePath));
await fs.writeFile(filePath, content);
return true;
} catch (err) {
console.error('Write error:', err);
return false;
}
}
/**
* Delete resource
* @param {string} urlPath
* @returns {Promise<boolean>}
*/
export async function remove(urlPath) {
const filePath = urlToPath(urlPath);
try {
await fs.remove(filePath);
return true;
} catch {
return false;
}
}
/**
* Create container (directory)
* @param {string} urlPath
* @returns {Promise<boolean>}
*/
export async function createContainer(urlPath) {
const filePath = urlToPath(urlPath);
try {
await fs.ensureDir(filePath);
return true;
} catch {
return false;
}
}
/**
* List container contents with stat metadata
* @param {string} urlPath
* @returns {Promise<Array<{name: string, isDirectory: boolean, size?: number, modified?: string}> | null>}
*/
export async function listContainer(urlPath) {
const filePath = urlToPath(urlPath);
try {
const entries = await fs.readdir(filePath, { withFileTypes: true });
const results = await Promise.all(entries.map(async (entry) => {
const result = {
name: entry.name,
isDirectory: entry.isDirectory()
};
try {
const stat = await fs.stat(path.join(filePath, entry.name));
result.size = stat.size;
result.modified = stat.mtime.toISOString();
} catch { /* stat failed, skip metadata */ }
return result;
}));
return results;
} catch {
return null;
}
}
/**
* Generate unique filename for POST
* @param {string} containerPath
* @param {string} slug
* @param {boolean} isDir
* @returns {Promise<string>}
*/
export async function generateUniqueFilename(containerPath, slug, isDir = false) {
const basePath = urlToPath(containerPath);
let name = slug || crypto.randomUUID();
// Security: Remove any path traversal attempts and problematic characters
name = name.replace(/[/\\]/g, '-');
name = name.replace(/\.\./g, ''); // Remove .. sequences
// Security: Limit filename length
if (name.length > 255) {
name = name.substring(0, 255);
}
let candidate = path.join(basePath, name);
let counter = 1;
while (await fs.pathExists(candidate)) {
const ext = path.extname(name);
const base = path.basename(name, ext);
candidate = path.join(basePath, `${base}-${counter}${ext}`);
counter++;
}
return path.basename(candidate);
}