qodalis-cli-server-plugin-filesystem-sqlite 1.0.0b28__tar.gz → 1.0.0b30__tar.gz
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.
- {qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b28 → qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b30}/.gitignore +3 -0
- {qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b28 → qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b30}/PKG-INFO +3 -1
- {qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b28 → qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b30}/pyproject.toml +3 -1
- {qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b28 → qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b30}/qodalis_cli_filesystem_sqlite/__init__.py +2 -1
- {qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b28 → qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b30}/qodalis_cli_filesystem_sqlite/sqlite_provider.py +175 -116
- {qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b28 → qodalis_cli_server_plugin_filesystem_sqlite-1.0.0b30}/LICENSE +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: qodalis-cli-server-plugin-filesystem-sqlite
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.0b30
|
|
4
4
|
Summary: SQLite-based storage provider for the Qodalis CLI filesystem plugin
|
|
5
5
|
Project-URL: Homepage, https://github.com/qodalis-solutions/cli-server-python
|
|
6
6
|
Project-URL: Repository, https://github.com/qodalis-solutions/cli-server-python
|
|
@@ -18,4 +18,6 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
20
|
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: qodalis-cli-server-abstractions>=1.0.0b17
|
|
22
|
+
Requires-Dist: qodalis-cli-server-plugin-databases>=1.0.0b29
|
|
21
23
|
Requires-Dist: qodalis-cli-server-plugin-filesystem
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "qodalis-cli-server-plugin-filesystem-sqlite"
|
|
7
|
-
version = "1.0.
|
|
7
|
+
version = "1.0.0b30"
|
|
8
8
|
description = "SQLite-based storage provider for the Qodalis CLI filesystem plugin"
|
|
9
9
|
readme = { text = "", content-type = "text/plain" }
|
|
10
10
|
license = "MIT"
|
|
@@ -24,7 +24,9 @@ classifiers = [
|
|
|
24
24
|
"Programming Language :: Python :: 3.13",
|
|
25
25
|
]
|
|
26
26
|
dependencies = [
|
|
27
|
+
"qodalis-cli-server-abstractions>=1.0.0b17",
|
|
27
28
|
"qodalis-cli-server-plugin-filesystem",
|
|
29
|
+
"qodalis-cli-server-plugin-databases>=1.0.0b29",
|
|
28
30
|
]
|
|
29
31
|
|
|
30
32
|
[project.urls]
|
|
@@ -5,9 +5,10 @@ from __future__ import annotations
|
|
|
5
5
|
#: The package's icon on the admin dashboard's plugins page (``CliInstalledPlugin.icon``).
|
|
6
6
|
__plugin_icon__ = "🪶"
|
|
7
7
|
|
|
8
|
-
from .sqlite_provider import SqliteFileStorageProvider, SqliteProviderOptions
|
|
8
|
+
from .sqlite_provider import SqliteFileStorageProvider, SqliteProviderOptions, filesystem_sqlite_migrations
|
|
9
9
|
|
|
10
10
|
__all__ = [
|
|
11
11
|
"SqliteFileStorageProvider",
|
|
12
12
|
"SqliteProviderOptions",
|
|
13
|
+
"filesystem_sqlite_migrations",
|
|
13
14
|
]
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
+
import asyncio
|
|
5
6
|
import os
|
|
6
7
|
import sqlite3
|
|
7
|
-
from dataclasses import dataclass
|
|
8
|
+
from dataclasses import dataclass, field
|
|
8
9
|
from datetime import datetime, timezone
|
|
9
10
|
from typing import AsyncIterator
|
|
10
11
|
|
|
@@ -18,25 +19,72 @@ from qodalis_cli_filesystem import (
|
|
|
18
19
|
FileStoragePermissionError,
|
|
19
20
|
IFileStorageProvider,
|
|
20
21
|
)
|
|
22
|
+
from qodalis_cli_databases import (
|
|
23
|
+
CliMigration,
|
|
24
|
+
CliMigrationDatabaseOptions,
|
|
25
|
+
CliMigrationRunner,
|
|
26
|
+
CliMigrationRunOptions,
|
|
27
|
+
CliSqlConnection,
|
|
28
|
+
CliSqlSession,
|
|
29
|
+
CodeSource,
|
|
30
|
+
adopt_legacy_table,
|
|
31
|
+
code_source,
|
|
32
|
+
sqlite_connection,
|
|
33
|
+
table_exists,
|
|
34
|
+
)
|
|
35
|
+
from qodalis_cli_server_abstractions import CliTableNameOptions, qcli_table_name
|
|
36
|
+
|
|
37
|
+
#: The columns of a previous release (``files``), renamed by the first migration.
|
|
38
|
+
LEGACY_COLUMNS: dict[str, str] = {
|
|
39
|
+
"id": "Id",
|
|
40
|
+
"path": "Path",
|
|
41
|
+
"name": "Name",
|
|
42
|
+
"type": "Type",
|
|
43
|
+
"content": "Content",
|
|
44
|
+
"size": "Size",
|
|
45
|
+
"permissions": "Permissions",
|
|
46
|
+
"created_at": "CreatedAt",
|
|
47
|
+
"modified_at": "ModifiedAt",
|
|
48
|
+
"parent_path": "ParentPath",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def filesystem_sqlite_migrations(table: str, legacy: bool = False) -> CodeSource:
|
|
53
|
+
"""The schema of the files table as migrations (source ``qcli-filesystem``); ``table`` is unquoted."""
|
|
54
|
+
|
|
55
|
+
async def files_table(db: CliSqlSession) -> None:
|
|
56
|
+
if not legacy:
|
|
57
|
+
await adopt_legacy_table(db, "files", table, LEGACY_COLUMNS)
|
|
58
|
+
if await table_exists(db, table):
|
|
59
|
+
return
|
|
60
|
+
col = (lambda n: n.lower()) if legacy else (lambda n: n)
|
|
61
|
+
created, modified, parent = (
|
|
62
|
+
("created_at", "modified_at", "parent_path") if legacy else ("CreatedAt", "ModifiedAt", "ParentPath")
|
|
63
|
+
)
|
|
64
|
+
await db.exec(f"""CREATE TABLE "{table}" (
|
|
65
|
+
"{col('Id')}" INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
66
|
+
"{col('Path')}" TEXT NOT NULL UNIQUE,
|
|
67
|
+
"{col('Name')}" TEXT NOT NULL,
|
|
68
|
+
"{col('Type')}" TEXT NOT NULL CHECK("{col('Type')}" IN ('file', 'directory')),
|
|
69
|
+
"{col('Content')}" TEXT,
|
|
70
|
+
"{col('Size')}" INTEGER NOT NULL DEFAULT 0,
|
|
71
|
+
"{col('Permissions')}" TEXT DEFAULT '644',
|
|
72
|
+
"{created}" TEXT NOT NULL,
|
|
73
|
+
"{modified}" TEXT NOT NULL,
|
|
74
|
+
"{parent}" TEXT
|
|
75
|
+
)""")
|
|
76
|
+
|
|
77
|
+
async def parent_index(db: CliSqlSession) -> None:
|
|
78
|
+
if legacy:
|
|
79
|
+
await db.exec(f'CREATE INDEX IF NOT EXISTS "idx_files_parent" ON "{table}" ("parent_path")')
|
|
80
|
+
return
|
|
81
|
+
await db.exec('DROP INDEX IF EXISTS "idx_files_parent"')
|
|
82
|
+
await db.exec(f'CREATE INDEX IF NOT EXISTS "IX_{table}_ParentPath" ON "{table}" ("ParentPath")')
|
|
21
83
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
name TEXT NOT NULL,
|
|
27
|
-
type TEXT NOT NULL CHECK(type IN ('file', 'directory')),
|
|
28
|
-
content TEXT,
|
|
29
|
-
size INTEGER NOT NULL DEFAULT 0,
|
|
30
|
-
permissions TEXT DEFAULT '644',
|
|
31
|
-
created_at TEXT NOT NULL,
|
|
32
|
-
modified_at TEXT NOT NULL,
|
|
33
|
-
parent_path TEXT
|
|
34
|
-
);
|
|
35
|
-
"""
|
|
36
|
-
|
|
37
|
-
_CREATE_INDEX = """\
|
|
38
|
-
CREATE INDEX IF NOT EXISTS idx_files_parent ON files(parent_path);
|
|
39
|
-
"""
|
|
84
|
+
return code_source("qcli-filesystem", [
|
|
85
|
+
CliMigration("1", "files table", files_table),
|
|
86
|
+
CliMigration("2", "index on the parent path", parent_index),
|
|
87
|
+
])
|
|
40
88
|
|
|
41
89
|
|
|
42
90
|
@dataclass
|
|
@@ -44,6 +92,12 @@ class SqliteProviderOptions:
|
|
|
44
92
|
"""Configuration for the SQLite file storage provider."""
|
|
45
93
|
|
|
46
94
|
db_path: str = "./data/files.db"
|
|
95
|
+
#: Prefix of the table (default ``Qcli``: ``QcliFiles``); ``""`` gives ``Files``.
|
|
96
|
+
table_prefix: str | None = None
|
|
97
|
+
#: The table's name (``{"Files": "MyFiles"}``), winning over the prefix.
|
|
98
|
+
tables: dict[str, str] = field(default_factory=dict)
|
|
99
|
+
#: Keep the previous release's ``files`` table and columns instead of renaming them.
|
|
100
|
+
legacy_table_names: bool = False
|
|
47
101
|
|
|
48
102
|
|
|
49
103
|
def _now_iso() -> str:
|
|
@@ -83,6 +137,12 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
83
137
|
|
|
84
138
|
Uses Python's built-in ``sqlite3`` module with no external dependencies.
|
|
85
139
|
All interface methods are async but use synchronous sqlite3 calls internally.
|
|
140
|
+
|
|
141
|
+
The table is ``QcliFiles`` (PascalCase columns), versioned by the databases
|
|
142
|
+
plugin's migrations (source ``qcli-filesystem``): the first operation after an
|
|
143
|
+
upgrade renames the previous release's ``files`` table and its columns,
|
|
144
|
+
keeping the data. ``connection`` and ``migration_source`` show it in the
|
|
145
|
+
databases plugin.
|
|
86
146
|
"""
|
|
87
147
|
|
|
88
148
|
def __init__(self, options: SqliteProviderOptions | None = None) -> None:
|
|
@@ -95,46 +155,85 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
95
155
|
if parent_dir:
|
|
96
156
|
os.makedirs(parent_dir, exist_ok=True)
|
|
97
157
|
|
|
98
|
-
self._conn = sqlite3.connect(self._db_path)
|
|
158
|
+
self._conn = sqlite3.connect(self._db_path, check_same_thread=False)
|
|
99
159
|
self._conn.row_factory = sqlite3.Row
|
|
100
160
|
self._conn.execute("PRAGMA journal_mode=WAL")
|
|
101
|
-
self._conn.execute(_CREATE_TABLE)
|
|
102
|
-
self._conn.execute(_CREATE_INDEX)
|
|
103
|
-
self._conn.commit()
|
|
104
161
|
|
|
105
|
-
|
|
162
|
+
legacy = options.legacy_table_names
|
|
163
|
+
name = qcli_table_name(
|
|
164
|
+
"Files",
|
|
165
|
+
CliTableNameOptions(
|
|
166
|
+
table_prefix=options.table_prefix, tables=dict(options.tables or {}), legacy_table_names=legacy,
|
|
167
|
+
),
|
|
168
|
+
"files",
|
|
169
|
+
)
|
|
170
|
+
#: The files table, quoted, and its columns with an underscore in the previous release.
|
|
171
|
+
self._t = f'"{name}"'
|
|
172
|
+
created, modified, parent = (
|
|
173
|
+
("created_at", "modified_at", "parent_path") if legacy else ("CreatedAt", "ModifiedAt", "ParentPath")
|
|
174
|
+
)
|
|
175
|
+
self._created, self._modified, self._parent = f'"{created}"', f'"{modified}"', f'"{parent}"'
|
|
176
|
+
#: Every column, aliased to the names the provider reads.
|
|
177
|
+
self._row = (
|
|
178
|
+
f"id AS id, path AS path, name AS name, type AS type, content AS content, size AS size, "
|
|
179
|
+
f"permissions AS permissions, {self._created} AS created_at, {self._modified} AS modified_at, "
|
|
180
|
+
f"{self._parent} AS parent_path"
|
|
181
|
+
)
|
|
182
|
+
self._insert = (
|
|
183
|
+
f"INSERT INTO {self._t} (path, name, type, content, size, permissions, {self._created}, "
|
|
184
|
+
f"{self._modified}, {self._parent}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
|
185
|
+
)
|
|
186
|
+
#: The database, for the databases plugin (``add_database("files", ...)``).
|
|
187
|
+
self.connection: CliSqlConnection = sqlite_connection(self._conn)
|
|
188
|
+
#: The files table's migrations (``qcli-filesystem``), to show them in the databases plugin.
|
|
189
|
+
self.migration_source: CodeSource = filesystem_sqlite_migrations(name, legacy)
|
|
190
|
+
self._runner = CliMigrationRunner(
|
|
191
|
+
"files", CliMigrationDatabaseOptions(self.connection, table_prefix=options.table_prefix),
|
|
192
|
+
).add_source(self.migration_source)
|
|
193
|
+
self._ready = False
|
|
194
|
+
self._ready_lock: asyncio.Lock | None = None
|
|
195
|
+
|
|
196
|
+
async def _ensure_ready(self) -> None:
|
|
197
|
+
"""Migrates the schema and creates the root, once, before the first operation."""
|
|
198
|
+
if self._ready:
|
|
199
|
+
return
|
|
200
|
+
if self._ready_lock is None:
|
|
201
|
+
self._ready_lock = asyncio.Lock()
|
|
202
|
+
async with self._ready_lock:
|
|
203
|
+
if self._ready:
|
|
204
|
+
return
|
|
205
|
+
run = await self._runner.up(CliMigrationRunOptions(trigger="startup", wait=True))
|
|
206
|
+
if getattr(run, "status", None) == "failed":
|
|
207
|
+
raise RuntimeError(f"filesystem-sqlite schema: {run.error}") # type: ignore[union-attr]
|
|
208
|
+
self._ensure_root()
|
|
209
|
+
self._ready = True
|
|
106
210
|
|
|
107
211
|
def _ensure_root(self) -> None:
|
|
108
212
|
"""Insert the root directory row if it does not exist."""
|
|
109
|
-
row = self._conn.execute(
|
|
110
|
-
"SELECT id FROM files WHERE path = ?", ("/",)
|
|
111
|
-
).fetchone()
|
|
213
|
+
row = self._conn.execute(f"SELECT id FROM {self._t} WHERE path = ?", ("/",)).fetchone()
|
|
112
214
|
if row is None:
|
|
113
215
|
now = _now_iso()
|
|
114
|
-
self._conn.execute(
|
|
115
|
-
"INSERT INTO files (path, name, type, size, permissions, created_at, modified_at, parent_path) "
|
|
116
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
117
|
-
("/", "", "directory", 0, "drwxr-xr-x", now, now, None),
|
|
118
|
-
)
|
|
216
|
+
self._conn.execute(self._insert, ("/", "", "directory", None, 0, "drwxr-xr-x", now, now, None))
|
|
119
217
|
self._conn.commit()
|
|
120
218
|
|
|
219
|
+
def _get(self, path: str) -> sqlite3.Row | None:
|
|
220
|
+
return self._conn.execute(f"SELECT {self._row} FROM {self._t} WHERE path = ?", (path,)).fetchone()
|
|
221
|
+
|
|
121
222
|
@property
|
|
122
223
|
def name(self) -> str:
|
|
123
224
|
return "sqlite"
|
|
124
225
|
|
|
125
226
|
async def list(self, path: str) -> list[FileEntry]:
|
|
227
|
+
await self._ensure_ready()
|
|
126
228
|
norm = _normalize_path(path)
|
|
127
|
-
row = self.
|
|
128
|
-
"SELECT type FROM files WHERE path = ?", (norm,)
|
|
129
|
-
).fetchone()
|
|
229
|
+
row = self._get(norm)
|
|
130
230
|
if row is None:
|
|
131
231
|
raise FileStorageNotFoundError(path)
|
|
132
232
|
if row["type"] != "directory":
|
|
133
233
|
raise FileStorageNotADirectoryError(path)
|
|
134
234
|
|
|
135
235
|
rows = self._conn.execute(
|
|
136
|
-
"SELECT
|
|
137
|
-
"WHERE parent_path = ? ORDER BY name",
|
|
236
|
+
f"SELECT {self._row} FROM {self._t} WHERE {self._parent} = ? ORDER BY name",
|
|
138
237
|
(norm,),
|
|
139
238
|
).fetchall()
|
|
140
239
|
|
|
@@ -150,10 +249,9 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
150
249
|
]
|
|
151
250
|
|
|
152
251
|
async def read_file(self, path: str) -> str:
|
|
252
|
+
await self._ensure_ready()
|
|
153
253
|
norm = _normalize_path(path)
|
|
154
|
-
row = self.
|
|
155
|
-
"SELECT type, content FROM files WHERE path = ?", (norm,)
|
|
156
|
-
).fetchone()
|
|
254
|
+
row = self._get(norm)
|
|
157
255
|
if row is None:
|
|
158
256
|
raise FileStorageNotFoundError(path)
|
|
159
257
|
if row["type"] == "directory":
|
|
@@ -161,6 +259,7 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
161
259
|
return row["content"] or ""
|
|
162
260
|
|
|
163
261
|
async def write_file(self, path: str, content: str | bytes) -> None:
|
|
262
|
+
await self._ensure_ready()
|
|
164
263
|
norm = _normalize_path(path)
|
|
165
264
|
if norm == "/":
|
|
166
265
|
raise FileStorageIsADirectoryError(path)
|
|
@@ -176,31 +275,23 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
176
275
|
await self._ensure_parents(parent)
|
|
177
276
|
|
|
178
277
|
# Check if it already exists
|
|
179
|
-
existing = self.
|
|
180
|
-
"SELECT type FROM files WHERE path = ?", (norm,)
|
|
181
|
-
).fetchone()
|
|
278
|
+
existing = self._get(norm)
|
|
182
279
|
if existing is not None and existing["type"] == "directory":
|
|
183
280
|
raise FileStorageIsADirectoryError(path)
|
|
184
281
|
|
|
185
282
|
if existing is not None:
|
|
186
283
|
self._conn.execute(
|
|
187
|
-
"UPDATE
|
|
284
|
+
f"UPDATE {self._t} SET content = ?, size = ?, {self._modified} = ? WHERE path = ?",
|
|
188
285
|
(text, size, now, norm),
|
|
189
286
|
)
|
|
190
287
|
else:
|
|
191
|
-
self._conn.execute(
|
|
192
|
-
"INSERT INTO files (path, name, type, content, size, permissions, created_at, modified_at, parent_path) "
|
|
193
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
194
|
-
(norm, file_name, "file", text, size, "-rw-r--r--", now, now, parent),
|
|
195
|
-
)
|
|
288
|
+
self._conn.execute(self._insert, (norm, file_name, "file", text, size, "-rw-r--r--", now, now, parent))
|
|
196
289
|
self._conn.commit()
|
|
197
290
|
|
|
198
291
|
async def stat(self, path: str) -> FileStat:
|
|
292
|
+
await self._ensure_ready()
|
|
199
293
|
norm = _normalize_path(path)
|
|
200
|
-
row = self.
|
|
201
|
-
"SELECT name, type, size, created_at, modified_at, permissions FROM files WHERE path = ?",
|
|
202
|
-
(norm,),
|
|
203
|
-
).fetchone()
|
|
294
|
+
row = self._get(norm)
|
|
204
295
|
if row is None:
|
|
205
296
|
raise FileStorageNotFoundError(path)
|
|
206
297
|
return FileStat(
|
|
@@ -213,6 +304,7 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
213
304
|
)
|
|
214
305
|
|
|
215
306
|
async def mkdir(self, path: str, recursive: bool = False) -> None:
|
|
307
|
+
await self._ensure_ready()
|
|
216
308
|
norm = _normalize_path(path)
|
|
217
309
|
if norm == "/":
|
|
218
310
|
return # root always exists
|
|
@@ -223,17 +315,13 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
223
315
|
parent = _parent_path(norm)
|
|
224
316
|
# Check parent exists
|
|
225
317
|
if parent:
|
|
226
|
-
p_row = self.
|
|
227
|
-
"SELECT type FROM files WHERE path = ?", (parent,)
|
|
228
|
-
).fetchone()
|
|
318
|
+
p_row = self._get(parent)
|
|
229
319
|
if p_row is None:
|
|
230
320
|
raise FileStorageNotFoundError(path)
|
|
231
321
|
if p_row["type"] != "directory":
|
|
232
322
|
raise FileStorageNotADirectoryError(path)
|
|
233
323
|
|
|
234
|
-
existing = self.
|
|
235
|
-
"SELECT type FROM files WHERE path = ?", (norm,)
|
|
236
|
-
).fetchone()
|
|
324
|
+
existing = self._get(norm)
|
|
237
325
|
if existing is not None:
|
|
238
326
|
if existing["type"] != "directory":
|
|
239
327
|
raise FileStorageExistsError(path)
|
|
@@ -241,52 +329,43 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
241
329
|
|
|
242
330
|
dir_name = norm.rsplit("/", 1)[-1]
|
|
243
331
|
now = _now_iso()
|
|
244
|
-
self._conn.execute(
|
|
245
|
-
"INSERT INTO files (path, name, type, size, permissions, created_at, modified_at, parent_path) "
|
|
246
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
247
|
-
(norm, dir_name, "directory", 0, "drwxr-xr-x", now, now, parent),
|
|
248
|
-
)
|
|
332
|
+
self._conn.execute(self._insert, (norm, dir_name, "directory", None, 0, "drwxr-xr-x", now, now, parent))
|
|
249
333
|
self._conn.commit()
|
|
250
334
|
|
|
251
335
|
async def remove(self, path: str, recursive: bool = False) -> None:
|
|
336
|
+
await self._ensure_ready()
|
|
252
337
|
norm = _normalize_path(path)
|
|
253
338
|
if norm == "/":
|
|
254
339
|
raise FileStoragePermissionError(path)
|
|
255
340
|
|
|
256
|
-
row = self.
|
|
257
|
-
"SELECT type FROM files WHERE path = ?", (norm,)
|
|
258
|
-
).fetchone()
|
|
341
|
+
row = self._get(norm)
|
|
259
342
|
if row is None:
|
|
260
343
|
raise FileStorageNotFoundError(path)
|
|
261
344
|
|
|
262
345
|
if row["type"] == "directory" and not recursive:
|
|
263
346
|
# Check if directory has children
|
|
264
347
|
child = self._conn.execute(
|
|
265
|
-
"SELECT id FROM
|
|
348
|
+
f"SELECT id FROM {self._t} WHERE {self._parent} = ? LIMIT 1", (norm,)
|
|
266
349
|
).fetchone()
|
|
267
350
|
if child is not None:
|
|
268
351
|
raise FileStorageNotADirectoryError(path)
|
|
269
352
|
|
|
270
353
|
if recursive:
|
|
271
354
|
# Delete the entry and all descendants (path starts with norm/)
|
|
272
|
-
self._conn.execute(
|
|
273
|
-
"DELETE FROM files WHERE path = ? OR path LIKE ?",
|
|
274
|
-
(norm, norm + "/%"),
|
|
275
|
-
)
|
|
355
|
+
self._conn.execute(f"DELETE FROM {self._t} WHERE path = ? OR path LIKE ?", (norm, norm + "/%"))
|
|
276
356
|
else:
|
|
277
|
-
self._conn.execute("DELETE FROM
|
|
357
|
+
self._conn.execute(f"DELETE FROM {self._t} WHERE path = ?", (norm,))
|
|
278
358
|
self._conn.commit()
|
|
279
359
|
|
|
280
360
|
async def copy(self, src: str, dest: str) -> None:
|
|
361
|
+
await self._ensure_ready()
|
|
281
362
|
src_norm = _normalize_path(src)
|
|
282
363
|
dest_norm = _normalize_path(dest)
|
|
283
364
|
|
|
284
365
|
if dest_norm == "/":
|
|
285
366
|
raise FileStorageIsADirectoryError(dest)
|
|
286
367
|
|
|
287
|
-
src_row = self.
|
|
288
|
-
"SELECT * FROM files WHERE path = ?", (src_norm,)
|
|
289
|
-
).fetchone()
|
|
368
|
+
src_row = self._get(src_norm)
|
|
290
369
|
if src_row is None:
|
|
291
370
|
raise FileStorageNotFoundError(src)
|
|
292
371
|
|
|
@@ -298,36 +377,33 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
298
377
|
dest_name = dest_norm.rsplit("/", 1)[-1]
|
|
299
378
|
|
|
300
379
|
# Remove existing destination if any
|
|
301
|
-
self._conn.execute("DELETE FROM
|
|
380
|
+
self._conn.execute(f"DELETE FROM {self._t} WHERE path = ?", (dest_norm,))
|
|
302
381
|
|
|
303
382
|
# Copy the source entry
|
|
304
|
-
self._conn.execute(
|
|
305
|
-
|
|
306
|
-
"
|
|
307
|
-
|
|
308
|
-
src_row["size"], src_row["permissions"], now, now, dest_parent),
|
|
309
|
-
)
|
|
383
|
+
self._conn.execute(self._insert, (
|
|
384
|
+
dest_norm, dest_name, src_row["type"], src_row["content"],
|
|
385
|
+
src_row["size"], src_row["permissions"], now, now, dest_parent,
|
|
386
|
+
))
|
|
310
387
|
|
|
311
388
|
# If directory, copy all descendants
|
|
312
389
|
if src_row["type"] == "directory":
|
|
313
390
|
descendants = self._conn.execute(
|
|
314
|
-
"SELECT
|
|
391
|
+
f"SELECT {self._row} FROM {self._t} WHERE path LIKE ?", (src_norm + "/%",)
|
|
315
392
|
).fetchall()
|
|
316
393
|
for desc in descendants:
|
|
317
394
|
new_path = dest_norm + desc["path"][len(src_norm):]
|
|
318
395
|
new_parent = _parent_path(new_path)
|
|
319
396
|
# Remove existing at new path
|
|
320
|
-
self._conn.execute("DELETE FROM
|
|
321
|
-
self._conn.execute(
|
|
322
|
-
|
|
323
|
-
"
|
|
324
|
-
|
|
325
|
-
desc["size"], desc["permissions"], now, now, new_parent),
|
|
326
|
-
)
|
|
397
|
+
self._conn.execute(f"DELETE FROM {self._t} WHERE path = ?", (new_path,))
|
|
398
|
+
self._conn.execute(self._insert, (
|
|
399
|
+
new_path, desc["name"], desc["type"], desc["content"],
|
|
400
|
+
desc["size"], desc["permissions"], now, now, new_parent,
|
|
401
|
+
))
|
|
327
402
|
|
|
328
403
|
self._conn.commit()
|
|
329
404
|
|
|
330
405
|
async def move(self, src: str, dest: str) -> None:
|
|
406
|
+
await self._ensure_ready()
|
|
331
407
|
src_norm = _normalize_path(src)
|
|
332
408
|
if src_norm == "/":
|
|
333
409
|
raise FileStoragePermissionError(src)
|
|
@@ -336,34 +412,24 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
336
412
|
if dest_norm == "/":
|
|
337
413
|
raise FileStorageIsADirectoryError(dest)
|
|
338
414
|
|
|
339
|
-
|
|
340
|
-
"SELECT * FROM files WHERE path = ?", (src_norm,)
|
|
341
|
-
).fetchone()
|
|
342
|
-
if src_row is None:
|
|
415
|
+
if self._get(src_norm) is None:
|
|
343
416
|
raise FileStorageNotFoundError(src)
|
|
344
417
|
|
|
345
418
|
# Copy then remove source
|
|
346
419
|
await self.copy(src, dest)
|
|
347
420
|
|
|
348
421
|
# Remove source and its descendants
|
|
349
|
-
self._conn.execute(
|
|
350
|
-
"DELETE FROM files WHERE path = ? OR path LIKE ?",
|
|
351
|
-
(src_norm, src_norm + "/%"),
|
|
352
|
-
)
|
|
422
|
+
self._conn.execute(f"DELETE FROM {self._t} WHERE path = ? OR path LIKE ?", (src_norm, src_norm + "/%"))
|
|
353
423
|
self._conn.commit()
|
|
354
424
|
|
|
355
425
|
async def exists(self, path: str) -> bool:
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
"SELECT id FROM files WHERE path = ?", (norm,)
|
|
359
|
-
).fetchone()
|
|
360
|
-
return row is not None
|
|
426
|
+
await self._ensure_ready()
|
|
427
|
+
return self._get(_normalize_path(path)) is not None
|
|
361
428
|
|
|
362
429
|
async def get_download_stream(self, path: str) -> AsyncIterator[bytes]:
|
|
430
|
+
await self._ensure_ready()
|
|
363
431
|
norm = _normalize_path(path)
|
|
364
|
-
row = self.
|
|
365
|
-
"SELECT type, content FROM files WHERE path = ?", (norm,)
|
|
366
|
-
).fetchone()
|
|
432
|
+
row = self._get(norm)
|
|
367
433
|
if row is None:
|
|
368
434
|
raise FileStorageNotFoundError(path)
|
|
369
435
|
if row["type"] == "directory":
|
|
@@ -390,18 +456,11 @@ class SqliteFileStorageProvider(IFileStorageProvider):
|
|
|
390
456
|
current = ""
|
|
391
457
|
for part in parts:
|
|
392
458
|
current = current + "/" + part
|
|
393
|
-
existing = self.
|
|
394
|
-
"SELECT type FROM files WHERE path = ?", (current,)
|
|
395
|
-
).fetchone()
|
|
459
|
+
existing = self._get(current)
|
|
396
460
|
if existing is not None:
|
|
397
461
|
if existing["type"] != "directory":
|
|
398
462
|
raise FileStorageNotADirectoryError(current)
|
|
399
463
|
continue
|
|
400
464
|
now = _now_iso()
|
|
401
|
-
|
|
402
|
-
self._conn.execute(
|
|
403
|
-
"INSERT INTO files (path, name, type, size, permissions, created_at, modified_at, parent_path) "
|
|
404
|
-
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
405
|
-
(current, part, "directory", 0, "drwxr-xr-x", now, now, parent),
|
|
406
|
-
)
|
|
465
|
+
self._conn.execute(self._insert, (current, part, "directory", None, 0, "drwxr-xr-x", now, now, _parent_path(current)))
|
|
407
466
|
self._conn.commit()
|