@axium/storage 0.1.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.md +157 -0
- package/README.md +17 -0
- package/dist/client.d.ts +15 -0
- package/dist/client.js +63 -0
- package/dist/common.d.ts +65 -0
- package/dist/common.js +13 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/plugin.d.ts +57 -0
- package/dist/plugin.js +62 -0
- package/dist/polyfills.d.ts +62 -0
- package/dist/polyfills.js +24 -0
- package/dist/selection.d.ts +17 -0
- package/dist/selection.js +32 -0
- package/dist/server.d.ts +61 -0
- package/dist/server.js +311 -0
- package/lib/StorageItemList.svelte +121 -0
- package/lib/StorageSidebar.svelte +41 -0
- package/lib/StorageSidebarItem.svelte +170 -0
- package/lib/tsconfig.json +10 -0
- package/package.json +44 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
|
|
3
|
+
Version 3, 29 June 2007
|
|
4
|
+
|
|
5
|
+
Copyright (C) 2007 Free Software Foundation, Inc.
|
|
6
|
+
<https://fsf.org/>
|
|
7
|
+
|
|
8
|
+
Everyone is permitted to copy and distribute verbatim copies of this
|
|
9
|
+
license document, but changing it is not allowed.
|
|
10
|
+
|
|
11
|
+
This version of the GNU Lesser General Public License incorporates the
|
|
12
|
+
terms and conditions of version 3 of the GNU General Public License,
|
|
13
|
+
supplemented by the additional permissions listed below.
|
|
14
|
+
|
|
15
|
+
## 0. Additional Definitions.
|
|
16
|
+
|
|
17
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
18
|
+
General Public License, and the "GNU GPL" refers to version 3 of the
|
|
19
|
+
GNU General Public License.
|
|
20
|
+
|
|
21
|
+
"The Library" refers to a covered work governed by this License, other
|
|
22
|
+
than an Application or a Combined Work as defined below.
|
|
23
|
+
|
|
24
|
+
An "Application" is any work that makes use of an interface provided
|
|
25
|
+
by the Library, but which is not otherwise based on the Library.
|
|
26
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
27
|
+
of using an interface provided by the Library.
|
|
28
|
+
|
|
29
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
30
|
+
Application with the Library. The particular version of the Library
|
|
31
|
+
with which the Combined Work was made is also called the "Linked
|
|
32
|
+
Version".
|
|
33
|
+
|
|
34
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
35
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
36
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
37
|
+
based on the Application, and not on the Linked Version.
|
|
38
|
+
|
|
39
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
40
|
+
object code and/or source code for the Application, including any data
|
|
41
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
42
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
43
|
+
|
|
44
|
+
## 1. Exception to Section 3 of the GNU GPL.
|
|
45
|
+
|
|
46
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
47
|
+
without being bound by section 3 of the GNU GPL.
|
|
48
|
+
|
|
49
|
+
## 2. Conveying Modified Versions.
|
|
50
|
+
|
|
51
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
52
|
+
facility refers to a function or data to be supplied by an Application
|
|
53
|
+
that uses the facility (other than as an argument passed when the
|
|
54
|
+
facility is invoked), then you may convey a copy of the modified
|
|
55
|
+
version:
|
|
56
|
+
|
|
57
|
+
- a) under this License, provided that you make a good faith effort
|
|
58
|
+
to ensure that, in the event an Application does not supply the
|
|
59
|
+
function or data, the facility still operates, and performs
|
|
60
|
+
whatever part of its purpose remains meaningful, or
|
|
61
|
+
- b) under the GNU GPL, with none of the additional permissions of
|
|
62
|
+
this License applicable to that copy.
|
|
63
|
+
|
|
64
|
+
## 3. Object Code Incorporating Material from Library Header Files.
|
|
65
|
+
|
|
66
|
+
The object code form of an Application may incorporate material from a
|
|
67
|
+
header file that is part of the Library. You may convey such object
|
|
68
|
+
code under terms of your choice, provided that, if the incorporated
|
|
69
|
+
material is not limited to numerical parameters, data structure
|
|
70
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
71
|
+
(ten or fewer lines in length), you do both of the following:
|
|
72
|
+
|
|
73
|
+
- a) Give prominent notice with each copy of the object code that
|
|
74
|
+
the Library is used in it and that the Library and its use are
|
|
75
|
+
covered by this License.
|
|
76
|
+
- b) Accompany the object code with a copy of the GNU GPL and this
|
|
77
|
+
license document.
|
|
78
|
+
|
|
79
|
+
## 4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that, taken
|
|
82
|
+
together, effectively do not restrict modification of the portions of
|
|
83
|
+
the Library contained in the Combined Work and reverse engineering for
|
|
84
|
+
debugging such modifications, if you also do each of the following:
|
|
85
|
+
|
|
86
|
+
- a) Give prominent notice with each copy of the Combined Work that
|
|
87
|
+
the Library is used in it and that the Library and its use are
|
|
88
|
+
covered by this License.
|
|
89
|
+
- b) Accompany the Combined Work with a copy of the GNU GPL and this
|
|
90
|
+
license document.
|
|
91
|
+
- c) For a Combined Work that displays copyright notices during
|
|
92
|
+
execution, include the copyright notice for the Library among
|
|
93
|
+
these notices, as well as a reference directing the user to the
|
|
94
|
+
copies of the GNU GPL and this license document.
|
|
95
|
+
- d) Do one of the following:
|
|
96
|
+
- 0) Convey the Minimal Corresponding Source under the terms of
|
|
97
|
+
this License, and the Corresponding Application Code in a form
|
|
98
|
+
suitable for, and under terms that permit, the user to
|
|
99
|
+
recombine or relink the Application with a modified version of
|
|
100
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
101
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
102
|
+
Corresponding Source.
|
|
103
|
+
- 1) Use a suitable shared library mechanism for linking with
|
|
104
|
+
the Library. A suitable mechanism is one that (a) uses at run
|
|
105
|
+
time a copy of the Library already present on the user's
|
|
106
|
+
computer system, and (b) will operate properly with a modified
|
|
107
|
+
version of the Library that is interface-compatible with the
|
|
108
|
+
Linked Version.
|
|
109
|
+
- e) Provide Installation Information, but only if you would
|
|
110
|
+
otherwise be required to provide such information under section 6
|
|
111
|
+
of the GNU GPL, and only to the extent that such information is
|
|
112
|
+
necessary to install and execute a modified version of the
|
|
113
|
+
Combined Work produced by recombining or relinking the Application
|
|
114
|
+
with a modified version of the Linked Version. (If you use option
|
|
115
|
+
4d0, the Installation Information must accompany the Minimal
|
|
116
|
+
Corresponding Source and Corresponding Application Code. If you
|
|
117
|
+
use option 4d1, you must provide the Installation Information in
|
|
118
|
+
the manner specified by section 6 of the GNU GPL for conveying
|
|
119
|
+
Corresponding Source.)
|
|
120
|
+
|
|
121
|
+
## 5. Combined Libraries.
|
|
122
|
+
|
|
123
|
+
You may place library facilities that are a work based on the Library
|
|
124
|
+
side by side in a single library together with other library
|
|
125
|
+
facilities that are not Applications and are not covered by this
|
|
126
|
+
License, and convey such a combined library under terms of your
|
|
127
|
+
choice, if you do both of the following:
|
|
128
|
+
|
|
129
|
+
- a) Accompany the combined library with a copy of the same work
|
|
130
|
+
based on the Library, uncombined with any other library
|
|
131
|
+
facilities, conveyed under the terms of this License.
|
|
132
|
+
- b) Give prominent notice with the combined library that part of it
|
|
133
|
+
is a work based on the Library, and explaining where to find the
|
|
134
|
+
accompanying uncombined form of the same work.
|
|
135
|
+
|
|
136
|
+
## 6. Revised Versions of the GNU Lesser General Public License.
|
|
137
|
+
|
|
138
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
139
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
140
|
+
versions will be similar in spirit to the present version, but may
|
|
141
|
+
differ in detail to address new problems or concerns.
|
|
142
|
+
|
|
143
|
+
Each version is given a distinguishing version number. If the Library
|
|
144
|
+
as you received it specifies that a certain numbered version of the
|
|
145
|
+
GNU Lesser General Public License "or any later version" applies to
|
|
146
|
+
it, you have the option of following the terms and conditions either
|
|
147
|
+
of that published version or of any later version published by the
|
|
148
|
+
Free Software Foundation. If the Library as you received it does not
|
|
149
|
+
specify a version number of the GNU Lesser General Public License, you
|
|
150
|
+
may choose any version of the GNU Lesser General Public License ever
|
|
151
|
+
published by the Free Software Foundation.
|
|
152
|
+
|
|
153
|
+
If the Library as you received it specifies that a proxy can decide
|
|
154
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
155
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
156
|
+
permanent authorization for you to choose that version for the
|
|
157
|
+
Library.
|
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Axium Storage
|
|
2
|
+
|
|
3
|
+
This is a plugin for allowing users to store data on an Axium server.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Update the configuration to include the files data directory:
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"storage": {
|
|
12
|
+
"data": "/path/to/storage/data"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Also, make sure to run `axium plugin init @axium/storage` to add the necessary database tables for the plugin.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { StorageItemMetadata, StorageItemUpdate, UserFilesInfo } from './common.js';
|
|
2
|
+
import type { ItemSelection } from './selection.js';
|
|
3
|
+
export declare function uploadItem(file: File): Promise<StorageItemMetadata>;
|
|
4
|
+
export declare function updateItem(fileId: string, data: Blob): Promise<StorageItemMetadata>;
|
|
5
|
+
export declare function getItemMetadata(fileId: string): Promise<StorageItemMetadata>;
|
|
6
|
+
export declare function getDirectoryMetadata(parentId: string): Promise<StorageItemMetadata[]>;
|
|
7
|
+
export declare function downloadItem(fileId: string): Promise<Blob>;
|
|
8
|
+
export declare function updateItemMetadata(fileId: string, metadata: StorageItemUpdate): Promise<StorageItemMetadata>;
|
|
9
|
+
export declare function deleteItem(fileId: string): Promise<StorageItemMetadata>;
|
|
10
|
+
export declare function getUserFiles(userId: string): Promise<UserFilesInfo>;
|
|
11
|
+
export interface _Sidebar {
|
|
12
|
+
selection: ItemSelection<string, StorageItemMetadata>;
|
|
13
|
+
items: StorageItemMetadata[];
|
|
14
|
+
getDirectory(id: string, assignTo?: StorageItemMetadata[]): Promise<StorageItemMetadata[]>;
|
|
15
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { fetchAPI, token } from '@axium/client/requests';
|
|
2
|
+
async function _upload(method, url, data) {
|
|
3
|
+
const init = {
|
|
4
|
+
method,
|
|
5
|
+
headers: {
|
|
6
|
+
'Content-Type': data.type,
|
|
7
|
+
'Content-Length': data.size.toString(),
|
|
8
|
+
},
|
|
9
|
+
body: data,
|
|
10
|
+
};
|
|
11
|
+
if (data instanceof File)
|
|
12
|
+
init.headers['X-Name'] = data.name;
|
|
13
|
+
if (token)
|
|
14
|
+
init.headers.Authorization = 'Bearer ' + token;
|
|
15
|
+
const response = await fetch(url, init);
|
|
16
|
+
if (!response.headers.get('Content-Type')?.includes('application/json')) {
|
|
17
|
+
throw new Error(`Unexpected response type: ${response.headers.get('Content-Type')}`);
|
|
18
|
+
}
|
|
19
|
+
const json = await response.json().catch(() => ({ message: 'Unknown server error (invalid JSON response)' }));
|
|
20
|
+
if (!response.ok)
|
|
21
|
+
throw new Error(json.message);
|
|
22
|
+
json.modifiedAt = new Date(json.modifiedAt);
|
|
23
|
+
return json;
|
|
24
|
+
}
|
|
25
|
+
export async function uploadItem(file) {
|
|
26
|
+
return _upload('PUT', '/raw/storage', file);
|
|
27
|
+
}
|
|
28
|
+
export async function updateItem(fileId, data) {
|
|
29
|
+
return _upload('POST', '/raw/storage/' + fileId, data);
|
|
30
|
+
}
|
|
31
|
+
export async function getItemMetadata(fileId) {
|
|
32
|
+
const result = await fetchAPI('GET', 'storage/item/:id', undefined, fileId);
|
|
33
|
+
result.modifiedAt = new Date(result.modifiedAt);
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
export async function getDirectoryMetadata(parentId) {
|
|
37
|
+
const result = await fetchAPI('GET', 'storage/directory/:id', undefined, parentId);
|
|
38
|
+
for (const item of result) {
|
|
39
|
+
item.modifiedAt = new Date(item.modifiedAt);
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
export async function downloadItem(fileId) {
|
|
44
|
+
const response = await fetch('/raw/storage/' + fileId, {
|
|
45
|
+
headers: token ? { Authorization: 'Bearer ' + token } : {},
|
|
46
|
+
});
|
|
47
|
+
if (!response.ok)
|
|
48
|
+
throw new Error('Failed to download files: ' + response.statusText);
|
|
49
|
+
return await response.blob();
|
|
50
|
+
}
|
|
51
|
+
export async function updateItemMetadata(fileId, metadata) {
|
|
52
|
+
return fetchAPI('PATCH', 'storage/item/:id', metadata, fileId);
|
|
53
|
+
}
|
|
54
|
+
export async function deleteItem(fileId) {
|
|
55
|
+
return fetchAPI('DELETE', 'storage/item/:id', undefined, fileId);
|
|
56
|
+
}
|
|
57
|
+
export async function getUserFiles(userId) {
|
|
58
|
+
const result = await fetchAPI('GET', 'users/:id/storage', undefined, userId);
|
|
59
|
+
for (const item of result.items) {
|
|
60
|
+
item.modifiedAt = new Date(item.modifiedAt);
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
package/dist/common.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import * as z from 'zod';
|
|
2
|
+
declare module '@axium/core/api' {
|
|
3
|
+
interface _apiTypes {
|
|
4
|
+
'users/:id/storage': {
|
|
5
|
+
OPTIONS: {
|
|
6
|
+
usage: StorageUsage;
|
|
7
|
+
limits: StorageLimits;
|
|
8
|
+
};
|
|
9
|
+
GET: UserFilesInfo;
|
|
10
|
+
};
|
|
11
|
+
'storage/item/:id': {
|
|
12
|
+
GET: StorageItemMetadata;
|
|
13
|
+
DELETE: StorageItemMetadata;
|
|
14
|
+
PATCH: [z.input<typeof StorageItemUpdate>, StorageItemMetadata];
|
|
15
|
+
};
|
|
16
|
+
'storage/directory/:id': {
|
|
17
|
+
GET: StorageItemMetadata[];
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export interface StorageLimits {
|
|
22
|
+
/** The maximum size per item in MB */
|
|
23
|
+
item_size: number;
|
|
24
|
+
/** Maximum number of items per user */
|
|
25
|
+
user_items: number;
|
|
26
|
+
/** The maximum storage size per user in MB */
|
|
27
|
+
user_size: number;
|
|
28
|
+
}
|
|
29
|
+
export interface StorageUsage {
|
|
30
|
+
bytes: number;
|
|
31
|
+
items: number;
|
|
32
|
+
}
|
|
33
|
+
export interface UserFilesInfo {
|
|
34
|
+
items: StorageItemMetadata[];
|
|
35
|
+
limits: StorageLimits;
|
|
36
|
+
usage: StorageUsage;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* An update to file metadata.
|
|
40
|
+
*/
|
|
41
|
+
export declare const StorageItemUpdate: z.ZodObject<{
|
|
42
|
+
name: z.ZodOptional<z.ZodString>;
|
|
43
|
+
owner: z.ZodOptional<z.ZodUUID>;
|
|
44
|
+
trash: z.ZodOptional<z.ZodBoolean>;
|
|
45
|
+
restrict: z.ZodOptional<z.ZodBoolean>;
|
|
46
|
+
visibility: z.ZodOptional<z.ZodBoolean>;
|
|
47
|
+
}, z.core.$strip>;
|
|
48
|
+
export type StorageItemUpdate = z.infer<typeof StorageItemUpdate>;
|
|
49
|
+
export interface StorageItemMetadata {
|
|
50
|
+
createdAt: Date;
|
|
51
|
+
dataURL?: string;
|
|
52
|
+
hash: string;
|
|
53
|
+
id: string;
|
|
54
|
+
immutable: boolean;
|
|
55
|
+
modifiedAt: Date;
|
|
56
|
+
name: string | null;
|
|
57
|
+
userId: string;
|
|
58
|
+
parentId: string | null;
|
|
59
|
+
/** Whether editing the file is restricted to the owner */
|
|
60
|
+
restricted: boolean;
|
|
61
|
+
size: number;
|
|
62
|
+
trashedAt: Date | null;
|
|
63
|
+
type: string;
|
|
64
|
+
visibility: number;
|
|
65
|
+
}
|
package/dist/common.js
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './common.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './common.js';
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Database, InitOptions, OpOptions } from '@axium/server/database';
|
|
2
|
+
import './common.js';
|
|
3
|
+
import './server.js';
|
|
4
|
+
declare function statusText(): Promise<string>;
|
|
5
|
+
declare function db_init(opt: InitOptions, db: Database): Promise<void>;
|
|
6
|
+
declare function db_wipe(opt: OpOptions, db: Database): Promise<void>;
|
|
7
|
+
declare function remove(opt: OpOptions, db: Database): Promise<void>;
|
|
8
|
+
declare function clean(opt: OpOptions, db: Database): Promise<void>;
|
|
9
|
+
declare const _default: {
|
|
10
|
+
statusText: typeof statusText;
|
|
11
|
+
hooks: {
|
|
12
|
+
db_init: typeof db_init;
|
|
13
|
+
db_wipe: typeof db_wipe;
|
|
14
|
+
remove: typeof remove;
|
|
15
|
+
clean: typeof clean;
|
|
16
|
+
};
|
|
17
|
+
name: string;
|
|
18
|
+
version: string;
|
|
19
|
+
author: string;
|
|
20
|
+
description: string;
|
|
21
|
+
funding: {
|
|
22
|
+
type: string;
|
|
23
|
+
url: string;
|
|
24
|
+
};
|
|
25
|
+
license: string;
|
|
26
|
+
repository: {
|
|
27
|
+
type: string;
|
|
28
|
+
url: string;
|
|
29
|
+
};
|
|
30
|
+
homepage: string;
|
|
31
|
+
bugs: {
|
|
32
|
+
url: string;
|
|
33
|
+
};
|
|
34
|
+
type: string;
|
|
35
|
+
main: string;
|
|
36
|
+
types: string;
|
|
37
|
+
exports: {
|
|
38
|
+
".": string;
|
|
39
|
+
"./*": string;
|
|
40
|
+
};
|
|
41
|
+
files: string[];
|
|
42
|
+
scripts: {
|
|
43
|
+
build: string;
|
|
44
|
+
};
|
|
45
|
+
peerDependencies: {
|
|
46
|
+
"@axium/client": string;
|
|
47
|
+
"@axium/core": string;
|
|
48
|
+
"@axium/server": string;
|
|
49
|
+
"@sveltejs/kit": string;
|
|
50
|
+
utilium: string;
|
|
51
|
+
};
|
|
52
|
+
dependencies: {
|
|
53
|
+
blakejs: string;
|
|
54
|
+
zod: string;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
export default _default;
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import config from '@axium/server/config';
|
|
2
|
+
import { count, database, warnExists } from '@axium/server/database';
|
|
3
|
+
import { done, start } from '@axium/server/io';
|
|
4
|
+
import { sql } from 'kysely';
|
|
5
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
6
|
+
import './common.js';
|
|
7
|
+
import './server.js';
|
|
8
|
+
import { formatBytes } from '@axium/core/format';
|
|
9
|
+
async function statusText() {
|
|
10
|
+
const items = await count('storage');
|
|
11
|
+
const { size } = await database
|
|
12
|
+
.selectFrom('storage')
|
|
13
|
+
.select(eb => eb.fn.sum('size').as('size'))
|
|
14
|
+
.executeTakeFirstOrThrow();
|
|
15
|
+
return `${items} items totaling ${formatBytes(Number(size))}`;
|
|
16
|
+
}
|
|
17
|
+
async function db_init(opt, db) {
|
|
18
|
+
start('Creating table storage');
|
|
19
|
+
await db.schema
|
|
20
|
+
.createTable('storage')
|
|
21
|
+
.addColumn('id', 'uuid', col => col.primaryKey().defaultTo(sql `gen_random_uuid()`))
|
|
22
|
+
.addColumn('userId', 'uuid', col => col.notNull().references('users.id').onDelete('cascade').onUpdate('cascade'))
|
|
23
|
+
.addColumn('parentId', 'uuid', col => col.references('storage.itemId').onDelete('cascade').onUpdate('cascade').defaultTo(null))
|
|
24
|
+
.addColumn('createdAt', 'timestamptz', col => col.notNull().defaultTo(sql `now()`))
|
|
25
|
+
.addColumn('modifiedAt', 'timestamptz', col => col.notNull().defaultTo(sql `now()`))
|
|
26
|
+
.addColumn('restricted', 'boolean', col => col.notNull().defaultTo(false))
|
|
27
|
+
.addColumn('size', 'integer', col => col.notNull())
|
|
28
|
+
.addColumn('trashedAt', 'timestamptz', col => col.defaultTo(null))
|
|
29
|
+
.addColumn('hash', 'bytea', col => col.notNull())
|
|
30
|
+
.addColumn('name', 'text', col => col.defaultTo(null))
|
|
31
|
+
.addColumn('type', 'text', col => col.notNull())
|
|
32
|
+
.addColumn('immutable', 'boolean', col => col.notNull())
|
|
33
|
+
.addColumn('visibility', 'integer', col => col.notNull().defaultTo(0))
|
|
34
|
+
.execute()
|
|
35
|
+
.then(done)
|
|
36
|
+
.catch(warnExists);
|
|
37
|
+
}
|
|
38
|
+
async function db_wipe(opt, db) {
|
|
39
|
+
start('Removing data from user storage');
|
|
40
|
+
await db.deleteFrom('storage').execute();
|
|
41
|
+
done();
|
|
42
|
+
}
|
|
43
|
+
async function remove(opt, db) {
|
|
44
|
+
start('Dropping table storage');
|
|
45
|
+
await db.schema.dropTable('storage').execute();
|
|
46
|
+
done();
|
|
47
|
+
}
|
|
48
|
+
async function clean(opt, db) {
|
|
49
|
+
start('Removing expired trash items');
|
|
50
|
+
const nDaysAgo = new Date(Date.now() - 86400000 * config.storage.trash_duration);
|
|
51
|
+
await db
|
|
52
|
+
.deleteFrom('storage')
|
|
53
|
+
.where('trashedAt', 'is not', null)
|
|
54
|
+
.where('trashedAt', '<', nDaysAgo)
|
|
55
|
+
.executeTakeFirstOrThrow()
|
|
56
|
+
.then(done);
|
|
57
|
+
}
|
|
58
|
+
export default {
|
|
59
|
+
...pkg,
|
|
60
|
+
statusText,
|
|
61
|
+
hooks: { db_init, db_wipe, remove, clean },
|
|
62
|
+
};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
See:
|
|
3
|
+
https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64
|
|
4
|
+
https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Uint8Array/toHex
|
|
5
|
+
https://github.com/microsoft/TypeScript/pull/61696
|
|
6
|
+
https://github.com/microsoft/TypeScript/issues/61695
|
|
7
|
+
|
|
8
|
+
@todo Remove when TypeScript 5.9 is released
|
|
9
|
+
*/
|
|
10
|
+
declare global {
|
|
11
|
+
interface Uint8ArrayConstructor {
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new `Uint8Array` from a base64-encoded string.
|
|
14
|
+
* @param string The base64-encoded string.
|
|
15
|
+
* @param options If provided, specifies the alphabet and handling of the last chunk.
|
|
16
|
+
* @returns A new `Uint8Array` instance.
|
|
17
|
+
* @throws {SyntaxError} If the input string contains characters outside the specified alphabet, or if the last
|
|
18
|
+
* chunk is inconsistent with the `lastChunkHandling` option.
|
|
19
|
+
*/
|
|
20
|
+
fromBase64: (string: string) => Uint8Array;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new `Uint8Array` from a base16-encoded string.
|
|
23
|
+
* @returns A new `Uint8Array` instance.
|
|
24
|
+
*/
|
|
25
|
+
fromHex: (string: string) => Uint8Array;
|
|
26
|
+
}
|
|
27
|
+
interface Uint8Array<TArrayBuffer extends ArrayBufferLike> {
|
|
28
|
+
/**
|
|
29
|
+
* Converts the `Uint8Array` to a base64-encoded string.
|
|
30
|
+
* @param options If provided, sets the alphabet and padding behavior used.
|
|
31
|
+
* @returns A base64-encoded string.
|
|
32
|
+
*/
|
|
33
|
+
toBase64: () => string;
|
|
34
|
+
/**
|
|
35
|
+
* Sets the `Uint8Array` from a base64-encoded string.
|
|
36
|
+
* @param string The base64-encoded string.
|
|
37
|
+
* @param options If provided, specifies the alphabet and handling of the last chunk.
|
|
38
|
+
* @returns An object containing the number of bytes read and written.
|
|
39
|
+
* @throws {SyntaxError} If the input string contains characters outside the specified alphabet, or if the last
|
|
40
|
+
* chunk is inconsistent with the `lastChunkHandling` option.
|
|
41
|
+
*/
|
|
42
|
+
setFromBase64?: (string: string) => {
|
|
43
|
+
read: number;
|
|
44
|
+
written: number;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Converts the `Uint8Array` to a base16-encoded string.
|
|
48
|
+
* @returns A base16-encoded string.
|
|
49
|
+
*/
|
|
50
|
+
toHex: () => string;
|
|
51
|
+
/**
|
|
52
|
+
* Sets the `Uint8Array` from a base16-encoded string.
|
|
53
|
+
* @param string The base16-encoded string.
|
|
54
|
+
* @returns An object containing the number of bytes read and written.
|
|
55
|
+
*/
|
|
56
|
+
setFromHex?: (string: string) => {
|
|
57
|
+
read: number;
|
|
58
|
+
written: number;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
See:
|
|
3
|
+
https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64
|
|
4
|
+
https://developer.mozilla.org/Web/JavaScript/Reference/Global_Objects/Uint8Array/toHex
|
|
5
|
+
https://github.com/microsoft/TypeScript/pull/61696
|
|
6
|
+
https://github.com/microsoft/TypeScript/issues/61695
|
|
7
|
+
|
|
8
|
+
@todo Remove when TypeScript 5.9 is released
|
|
9
|
+
*/
|
|
10
|
+
import { output } from '@axium/server/io';
|
|
11
|
+
Uint8Array.prototype.toHex ??=
|
|
12
|
+
(output.warn('Using a polyfill of Uint8Array.prototype.toHex'),
|
|
13
|
+
function toHex() {
|
|
14
|
+
return [...this].map(b => b.toString(16).padStart(2, '0')).join('');
|
|
15
|
+
});
|
|
16
|
+
Uint8Array.fromHex ??=
|
|
17
|
+
(output.warn('Using a polyfill of Uint8Array.fromHex'),
|
|
18
|
+
function fromHex(hex) {
|
|
19
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
20
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
21
|
+
bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16);
|
|
22
|
+
}
|
|
23
|
+
return bytes;
|
|
24
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteSet } from 'svelte/reactivity';
|
|
2
|
+
export declare class ItemSelection<T, V extends {
|
|
3
|
+
id: T;
|
|
4
|
+
}> extends SvelteSet<T> {
|
|
5
|
+
/**
|
|
6
|
+
* The list of items that can be selected.
|
|
7
|
+
*/
|
|
8
|
+
items: V[];
|
|
9
|
+
constructor(
|
|
10
|
+
/**
|
|
11
|
+
* The list of items that can be selected.
|
|
12
|
+
*/
|
|
13
|
+
items: V[]);
|
|
14
|
+
last?: T;
|
|
15
|
+
toggle(id: T): boolean;
|
|
16
|
+
toggleRange(id: T): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { SvelteSet } from 'svelte/reactivity';
|
|
2
|
+
export class ItemSelection extends SvelteSet {
|
|
3
|
+
items;
|
|
4
|
+
constructor(
|
|
5
|
+
/**
|
|
6
|
+
* The list of items that can be selected.
|
|
7
|
+
*/
|
|
8
|
+
items) {
|
|
9
|
+
super([]);
|
|
10
|
+
this.items = items;
|
|
11
|
+
}
|
|
12
|
+
last;
|
|
13
|
+
toggle(id) {
|
|
14
|
+
const has = this.has(id);
|
|
15
|
+
if (has)
|
|
16
|
+
this.delete(id);
|
|
17
|
+
else {
|
|
18
|
+
this.add(id);
|
|
19
|
+
this.last = id;
|
|
20
|
+
}
|
|
21
|
+
return has;
|
|
22
|
+
}
|
|
23
|
+
toggleRange(id) {
|
|
24
|
+
const from = this.items.findIndex(item => item.id === this.last);
|
|
25
|
+
const until = this.items.findIndex(item => item.id === id);
|
|
26
|
+
if (from === -1 || until === -1)
|
|
27
|
+
return;
|
|
28
|
+
const range = this.items.slice(Math.min(from, until), Math.max(from, until) + 1);
|
|
29
|
+
for (const item of range)
|
|
30
|
+
this.toggle(item.id);
|
|
31
|
+
}
|
|
32
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Generated } from 'kysely';
|
|
2
|
+
import type { StorageItemMetadata, StorageLimits, StorageUsage } from './common.js';
|
|
3
|
+
import './polyfills.js';
|
|
4
|
+
declare module '@axium/server/database' {
|
|
5
|
+
interface Schema {
|
|
6
|
+
storage: {
|
|
7
|
+
createdAt: Generated<Date>;
|
|
8
|
+
hash: Uint8Array;
|
|
9
|
+
id: Generated<string>;
|
|
10
|
+
immutable: Generated<boolean>;
|
|
11
|
+
modifiedAt: Generated<Date>;
|
|
12
|
+
name: string | null;
|
|
13
|
+
parentId: string | null;
|
|
14
|
+
restricted: Generated<boolean>;
|
|
15
|
+
size: number;
|
|
16
|
+
trashedAt: Date | null;
|
|
17
|
+
type: string;
|
|
18
|
+
userId: string;
|
|
19
|
+
visibility: Generated<number>;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
declare module '@axium/server/config' {
|
|
24
|
+
interface Config {
|
|
25
|
+
storage: {
|
|
26
|
+
/** Whether the storage API endpoints are enabled */
|
|
27
|
+
enabled: boolean;
|
|
28
|
+
/** Whether the files app is enabled. Requires `enabled` */
|
|
29
|
+
app_enabled: boolean;
|
|
30
|
+
/** Path to data directory */
|
|
31
|
+
data: string;
|
|
32
|
+
/** How many days files are kept in the trash */
|
|
33
|
+
trash_duration: number;
|
|
34
|
+
/** Default limits */
|
|
35
|
+
limits: StorageLimits;
|
|
36
|
+
/** Content Addressable Storage (CAS) configuration */
|
|
37
|
+
cas: {
|
|
38
|
+
/** Whether to use CAS */
|
|
39
|
+
enabled: boolean;
|
|
40
|
+
/** Mime types to include when determining if CAS should be used */
|
|
41
|
+
include: string[];
|
|
42
|
+
/** Mime types to exclude when determining if CAS should be used */
|
|
43
|
+
exclude: string[];
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export interface StorageItem extends StorageItemMetadata {
|
|
49
|
+
data: Uint8Array<ArrayBufferLike>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns the current usage of the storage for a user in bytes.
|
|
53
|
+
*/
|
|
54
|
+
export declare function currentUsage(userId: string): Promise<StorageUsage>;
|
|
55
|
+
export declare function get(itemId: string): Promise<StorageItemMetadata>;
|
|
56
|
+
export type ExternalLimitHandler = (userId?: string) => StorageLimits | Promise<StorageLimits>;
|
|
57
|
+
/**
|
|
58
|
+
* Define the handler to get limits for a user externally.
|
|
59
|
+
*/
|
|
60
|
+
export declare function useLimits(handler: ExternalLimitHandler): void;
|
|
61
|
+
export declare function getLimits(userId?: string): Promise<StorageLimits>;
|