-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
2,754 additions
and
296 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import type { LoginManager } from "./LoginManager"; | ||
import type { FileInfo } from "./Relay"; | ||
import type { FileInfoDAO, RelayManager } from "./RelayManager"; | ||
import type { SharedFolder } from "./SharedFolder"; | ||
import { customFetch } from "./customFetch"; | ||
import { getMimeType } from "./mimetypes"; | ||
import PocketBase from "pocketbase"; | ||
|
||
declare const AUTH_URL: string; | ||
|
||
export class ContentAddressedStore { | ||
private pb: PocketBase; | ||
|
||
constructor( | ||
private sharedFolder: SharedFolder, | ||
private relayManager: RelayManager, | ||
private loginManager: LoginManager, | ||
) { | ||
this.pb = new PocketBase(AUTH_URL, this.loginManager.authStore); | ||
} | ||
|
||
async listFiles(): Promise<FileInfo[]> { | ||
return this.relayManager.fileInfo | ||
.filter( | ||
(fileInfo) => fileInfo.sharedFolder.id === this.sharedFolder.remote?.id, | ||
) | ||
.values(); | ||
} | ||
|
||
async getByHash(hash: string): Promise<FileInfo | undefined> { | ||
const local = this.relayManager.fileInfo.find( | ||
(fileInfo) => fileInfo.synchash === hash, | ||
); | ||
if (local) { | ||
return local; | ||
} | ||
try { | ||
const records = await this.pb | ||
?.collection("file_info") | ||
.getFullList({ fetch: customFetch }); | ||
this.relayManager.store?.ingestBatch<FileInfo>(records); | ||
} catch (e) { | ||
// pass | ||
} | ||
return this.relayManager.fileInfo.find( | ||
(fileInfo) => fileInfo?.synchash === hash, | ||
); | ||
} | ||
|
||
async readFile(id: string): Promise<ArrayBuffer> { | ||
const fileInfo = this.relayManager.fileInfo.get(id); | ||
if (!fileInfo) throw new Error(`File not found: ${id}`); | ||
const response = await fileInfo.getAttachment(); | ||
return response.arrayBuffer; | ||
} | ||
|
||
async writeFile( | ||
item: Partial<FileInfoDAO>, | ||
content: ArrayBuffer | null, | ||
): Promise<FileInfo> { | ||
if (!this.sharedFolder.remote) { | ||
throw new Error("missing remote"); | ||
} | ||
const blob = | ||
content && item.name | ||
? new Blob([content], { type: getMimeType(item.name) }) | ||
: null; | ||
const fileData: Partial<FileInfoDAO<Blob>> = { | ||
relay: this.sharedFolder.remote.relay.id, | ||
shared_folder: this.sharedFolder.remote?.id, | ||
guid: item.guid, | ||
name: item.name, | ||
synchash: item.synchash, | ||
ctime: item.ctime, | ||
mtime: item.mtime, | ||
parent: item.parentId, | ||
is_directory: item.isDirectory, | ||
fileInfo: item.fileInfo, | ||
synctime: item.synctime || 0, | ||
}; | ||
if (blob) { | ||
fileData["attachment"] = blob; | ||
} | ||
|
||
const record = item.fileInfo | ||
? await this.pb.collection("file_info").update(item.fileInfo.id, fileData) | ||
: await this.pb.collection("file_info").create(fileData); | ||
const fileInfo = this.relayManager.store?.ingest<FileInfo>(record); | ||
if (!fileInfo) throw new Error("Failed to create file"); | ||
|
||
return fileInfo; | ||
} | ||
|
||
async deleteFile(id: string): Promise<void> { | ||
await this.pb.collection("file_info").delete(id); | ||
this.relayManager.store?.cascade("file_info", id); | ||
} | ||
|
||
public destroy() { | ||
this.pb.cancelAllRequests(); | ||
this.pb = null as any; | ||
this.relayManager = null as any; | ||
this.loginManager = null as any; | ||
this.sharedFolder = null as any; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface IFile { | ||
guid: string; | ||
path: string; | ||
move: (newPath: string) => void; | ||
connect: () => void; | ||
disconnect: () => void; | ||
destroy: () => void; | ||
} | ||
|
||
export interface Hashable { | ||
sha256: () => Promise<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.