@axium/storage 0.2.0 → 0.2.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/server.js +11 -30
- package/package.json +2 -2
package/dist/server.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Permission } from '@axium/core';
|
|
2
|
+
import { checkAuthForItem, checkAuthForUser, getSessionAndUser } from '@axium/server/auth';
|
|
2
3
|
import { addConfigDefaults, config } from '@axium/server/config';
|
|
3
4
|
import { connect, database, expectedTypes } from '@axium/server/database';
|
|
4
5
|
import { dirs } from '@axium/server/io';
|
|
5
|
-
import {
|
|
6
|
+
import { getToken, parseBody, withError } from '@axium/server/requests';
|
|
6
7
|
import { addRoute } from '@axium/server/routes';
|
|
7
8
|
import { error } from '@sveltejs/kit';
|
|
8
9
|
import { createHash } from 'node:crypto';
|
|
@@ -102,10 +103,7 @@ addRoute({
|
|
|
102
103
|
if (!config.storage.enabled)
|
|
103
104
|
error(503, 'User storage is disabled');
|
|
104
105
|
const itemId = event.params.id;
|
|
105
|
-
const item = await
|
|
106
|
-
if (!item)
|
|
107
|
-
error(404, 'Item not found');
|
|
108
|
-
await checkAuth(event, item.userId);
|
|
106
|
+
const { item } = await checkAuthForItem(event, 'storage', itemId, Permission.Read);
|
|
109
107
|
return item;
|
|
110
108
|
},
|
|
111
109
|
async PATCH(event) {
|
|
@@ -113,10 +111,7 @@ addRoute({
|
|
|
113
111
|
error(503, 'User storage is disabled');
|
|
114
112
|
const itemId = event.params.id;
|
|
115
113
|
const body = await parseBody(event, StorageItemUpdate);
|
|
116
|
-
|
|
117
|
-
if (!item)
|
|
118
|
-
error(404, 'Item not found');
|
|
119
|
-
await checkAuth(event, item.userId);
|
|
114
|
+
await checkAuthForItem(event, 'storage', itemId, Permission.Manage);
|
|
120
115
|
const values = {};
|
|
121
116
|
if ('publicPermission' in body)
|
|
122
117
|
values.publicPermission = body.publicPermission;
|
|
@@ -141,10 +136,7 @@ addRoute({
|
|
|
141
136
|
if (!config.storage.enabled)
|
|
142
137
|
error(503, 'User storage is disabled');
|
|
143
138
|
const itemId = event.params.id;
|
|
144
|
-
const item = await
|
|
145
|
-
if (!item)
|
|
146
|
-
error(404, 'Item not found');
|
|
147
|
-
await checkAuth(event, item.userId);
|
|
139
|
+
const { item } = await checkAuthForItem(event, 'storage', itemId, Permission.Manage);
|
|
148
140
|
await database
|
|
149
141
|
.deleteFrom('storage')
|
|
150
142
|
.where('id', '=', itemId)
|
|
@@ -168,10 +160,7 @@ addRoute({
|
|
|
168
160
|
if (!config.storage.enabled)
|
|
169
161
|
error(503, 'User storage is disabled');
|
|
170
162
|
const itemId = event.params.id;
|
|
171
|
-
const item = await
|
|
172
|
-
if (!item)
|
|
173
|
-
error(404, 'Item not found');
|
|
174
|
-
await checkAuth(event, item.userId);
|
|
163
|
+
const { item } = await checkAuthForItem(event, 'storage', itemId, Permission.Read);
|
|
175
164
|
if (item.type != 'inode/directory')
|
|
176
165
|
error(409, 'Item is not a directory');
|
|
177
166
|
const items = await database
|
|
@@ -257,10 +246,7 @@ addRoute({
|
|
|
257
246
|
if (!config.storage.enabled)
|
|
258
247
|
error(503, 'User storage is disabled');
|
|
259
248
|
const itemId = event.params.id;
|
|
260
|
-
const item = await
|
|
261
|
-
if (!item)
|
|
262
|
-
error(404, 'Item not found');
|
|
263
|
-
await checkAuth(event, item.userId);
|
|
249
|
+
const { item } = await checkAuthForItem(event, 'storage', itemId, Permission.Read);
|
|
264
250
|
if (item.trashedAt)
|
|
265
251
|
error(410, 'Trashed items can not be downloaded');
|
|
266
252
|
const content = new Uint8Array(readFileSync(join(config.storage.data, item.id)));
|
|
@@ -275,16 +261,11 @@ addRoute({
|
|
|
275
261
|
if (!config.storage.enabled)
|
|
276
262
|
error(503, 'User storage is disabled');
|
|
277
263
|
const itemId = event.params.id;
|
|
278
|
-
const item = await
|
|
279
|
-
if (!item)
|
|
280
|
-
error(404, 'Item not found');
|
|
281
|
-
const { accessor } = await checkAuth(event, item.userId);
|
|
264
|
+
const { item } = await checkAuthForItem(event, 'storage', itemId, Permission.Edit);
|
|
282
265
|
if (item.immutable)
|
|
283
266
|
error(403, 'Item is immutable');
|
|
284
267
|
if (item.trashedAt)
|
|
285
268
|
error(410, 'Trashed items can not be changed');
|
|
286
|
-
if (item.userId != accessor.id)
|
|
287
|
-
error(403, 'Item editing is restricted to the owner');
|
|
288
269
|
const type = event.request.headers.get('content-type') || 'application/octet-stream';
|
|
289
270
|
// @todo: add this to the audit log
|
|
290
271
|
if (type != item.type)
|
|
@@ -322,7 +303,7 @@ addRoute({
|
|
|
322
303
|
if (!config.storage.enabled)
|
|
323
304
|
error(503, 'User storage is disabled');
|
|
324
305
|
const userId = event.params.id;
|
|
325
|
-
await
|
|
306
|
+
await checkAuthForUser(event, userId);
|
|
326
307
|
const [usage, limits] = await Promise.all([currentUsage(userId), getLimits(userId)]).catch(withError('Could not fetch data'));
|
|
327
308
|
return { usage, limits };
|
|
328
309
|
},
|
|
@@ -330,7 +311,7 @@ addRoute({
|
|
|
330
311
|
if (!config.storage.enabled)
|
|
331
312
|
error(503, 'User storage is disabled');
|
|
332
313
|
const userId = event.params.id;
|
|
333
|
-
await
|
|
314
|
+
await checkAuthForUser(event, userId);
|
|
334
315
|
const [items, usage, limits] = await Promise.all([
|
|
335
316
|
database.selectFrom('storage').where('userId', '=', userId).selectAll().select(withEncodedHash).execute(),
|
|
336
317
|
currentUsage(userId),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@axium/storage",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"author": "James Prevett <axium@jamespre.dev> (https://jamespre.dev)",
|
|
5
5
|
"description": "User file storage for Axium",
|
|
6
6
|
"funding": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"@axium/client": ">=0.1.0",
|
|
35
35
|
"@axium/core": ">=0.5.0",
|
|
36
|
-
"@axium/server": ">=0.
|
|
36
|
+
"@axium/server": ">=0.18.0",
|
|
37
37
|
"@sveltejs/kit": "^2.23.0",
|
|
38
38
|
"utilium": "^2.3.8"
|
|
39
39
|
},
|