Skip to content

Commit

Permalink
chore: consistant naming of Uri
Browse files Browse the repository at this point in the history
  • Loading branch information
Manrich121 committed Dec 7, 2023
1 parent 764ed7e commit 4121153
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .changeset/polite-grapes-cover.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@journeyapps/powersync-attachments': patch
---

Change `AbstractAttachmentQueue` implementation to not save the full URI in the `attachments` table, instead create the full URI when needed.
Change `AbstractAttachmentQueue` implementation to not save the full URI in the `attachments` table, instead create it when needed.
4 changes: 2 additions & 2 deletions packages/powersync-attachments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ export class AttachmentQueue extends AbstractAttachmentQueue {
const photoAttachment = await this.newAttachmentRecord();
photoAttachment.local_uri = this.getLocalFilePathSuffix(photoAttachment.filename);

const localFilePathURI = this.getLocalUri(photoAttachment.local_uri);
const localFilePathUri = this.getLocalUri(photoAttachment.local_uri);

await this.storage.writeFile(localFilePathURI, base64Data, { encoding: 'base64' });
await this.storage.writeFile(localFilePathUri, base64Data, { encoding: 'base64' });

return this.saveToQueue(photoAttachment);
}
Expand Down
18 changes: 9 additions & 9 deletions packages/powersync-attachments/src/AbstractAttachmentQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
await this.powersync.writeTransaction(deleteRecord);
}

const localFilePathURI = this.getLocalUri(record.local_uri || this.getLocalFilePathSuffix(record.filename));
const localFilePathUri = this.getLocalUri(record.local_uri || this.getLocalFilePathSuffix(record.filename));

try {
// Delete file on storage
await this.storage.deleteFile(localFilePathURI, {
await this.storage.deleteFile(localFilePathUri, {
filename: record.filename
});
} catch (e) {
Expand All @@ -238,9 +238,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
throw new Error(`No local_uri for record ${JSON.stringify(record, null, 2)}`);
}

const localFilePathURI = this.getLocalUri(record.local_uri);
const localFilePathUri = this.getLocalUri(record.local_uri);
try {
if (!(await this.storage.fileExists(localFilePathURI))) {
if (!(await this.storage.fileExists(localFilePathUri))) {
console.warn(`File for ${record.id} does not exist, skipping upload`);
await this.update({
...record,
Expand All @@ -249,7 +249,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
return true;
}

const fileBuffer = await this.storage.readFile(localFilePathURI, {
const fileBuffer = await this.storage.readFile(localFilePathUri, {
encoding: EncodingType.Base64,
mediaType: record.media_type
});
Expand All @@ -276,8 +276,8 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
if (!record.local_uri) {
record.local_uri = this.getLocalFilePathSuffix(record.filename);
}
const localFilePathURI = this.getLocalUri(record.local_uri);
if (await this.storage.fileExists(localFilePathURI)) {
const localFilePathUri = this.getLocalUri(record.local_uri);
if (await this.storage.fileExists(localFilePathUri)) {
console.debug(`Local file already downloaded, marking "${record.id}" as synced`);
await this.update({ ...record, state: AttachmentState.SYNCED });
return true;
Expand All @@ -298,9 +298,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
});

// Ensure directory exists
await this.storage.makeDir(localFilePathURI.replace(record.filename, ''));
await this.storage.makeDir(localFilePathUri.replace(record.filename, ''));
// Write the file
await this.storage.writeFile(localFilePathURI, base64Data, {
await this.storage.writeFile(localFilePathUri, base64Data, {
encoding: EncodingType.Base64
});

Expand Down
23 changes: 6 additions & 17 deletions packages/powersync-attachments/src/StorageAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
export enum EncodingType {
UTF8 = "utf8",
Base64 = "base64",
UTF8 = 'utf8',
Base64 = 'base64'
}

export interface StorageAdapter {
uploadFile(
filePath: string,
data: ArrayBuffer,
options?: { mediaType?: string }
): Promise<void>;
uploadFile(filePath: string, data: ArrayBuffer, options?: { mediaType?: string }): Promise<void>;

downloadFile(filePath: string): Promise<Blob>;

writeFile(
fileURI: string,
base64Data: string,
options?: { encoding?: EncodingType }
): Promise<void>;
writeFile(fileUri: string, base64Data: string, options?: { encoding?: EncodingType }): Promise<void>;

readFile(
fileURI: string,
options?: { encoding?: EncodingType; mediaType?: string }
): Promise<ArrayBuffer>;
readFile(fileUri: string, options?: { encoding?: EncodingType; mediaType?: string }): Promise<ArrayBuffer>;

deleteFile(uri: string, options?: { filename?: string }): Promise<void>;

fileExists(fileURI: string): Promise<boolean>;
fileExists(fileUri: string): Promise<boolean>;

makeDir(uri: string): Promise<void>;

Expand Down

0 comments on commit 4121153

Please sign in to comment.