(
src: string,
dest: string,
opts?: { recursive?: boolean }
)
| 1262 | } |
| 1263 | |
| 1264 | async mv( |
| 1265 | src: string, |
| 1266 | dest: string, |
| 1267 | opts?: { recursive?: boolean } |
| 1268 | ): Promise<void> { |
| 1269 | await this.ensureInit(); |
| 1270 | const srcNorm = normalizePath(src); |
| 1271 | const destNorm = normalizePath(dest); |
| 1272 | const srcStat = await this.lstat(srcNorm); |
| 1273 | if (!srcStat) throw new Error(`ENOENT: no such file or directory: ${src}`); |
| 1274 | |
| 1275 | if (srcStat.type === "directory") { |
| 1276 | if (!(opts?.recursive ?? true)) { |
| 1277 | throw new Error( |
| 1278 | `EISDIR: cannot move directory without recursive: ${src}` |
| 1279 | ); |
| 1280 | } |
| 1281 | await this.cp(src, dest, { recursive: true }); |
| 1282 | await this.rm(src, { recursive: true, force: true }); |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | const destParent = getParent(destNorm); |
| 1287 | const destName = getBasename(destNorm); |
| 1288 | const T = this.tableName; |
| 1289 | await this.ensureParentDir(destParent); |
| 1290 | |
| 1291 | const existingDest = ( |
| 1292 | await this.sql.query<{ type: string }>( |
| 1293 | `SELECT type FROM ${T} WHERE path = ?`, |
| 1294 | destNorm |
| 1295 | ) |
| 1296 | )[0]; |
| 1297 | if (existingDest) { |
| 1298 | if (existingDest.type === "directory") { |
| 1299 | throw new Error(`EISDIR: cannot overwrite directory: ${dest}`); |
| 1300 | } |
| 1301 | await this.deleteFile(destNorm); |
| 1302 | } |
| 1303 | |
| 1304 | if (srcStat.type === "file") { |
| 1305 | const row = ( |
| 1306 | await this.sql.query<{ |
| 1307 | storage_backend: string; |
| 1308 | r2_key: string | null; |
| 1309 | }>(`SELECT storage_backend, r2_key FROM ${T} WHERE path = ?`, srcNorm) |
| 1310 | )[0]; |
| 1311 | if (row?.storage_backend === "r2" && row.r2_key) { |
| 1312 | const r2 = this.getR2(); |
| 1313 | if (r2) { |
| 1314 | const newKey = this.r2Key(destNorm); |
| 1315 | const obj = await r2.get(row.r2_key); |
| 1316 | if (obj) { |
| 1317 | await r2.put(newKey, await obj.arrayBuffer(), { |
| 1318 | httpMetadata: obj.httpMetadata |
| 1319 | }); |
| 1320 | } |
| 1321 | await r2.delete(row.r2_key); |
nothing calls this directly
no test coverage detected