2024-04-22 14:11:36 +00:00
|
|
|
// Copyright 2024 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { strictAssert } from '../../util/assert';
|
|
|
|
import type {
|
|
|
|
WebAPIType,
|
2024-05-14 17:04:50 +00:00
|
|
|
AttachmentV3ResponseType,
|
2024-04-22 14:11:36 +00:00
|
|
|
GetBackupInfoResponseType,
|
|
|
|
BackupMediaItemType,
|
|
|
|
BackupMediaBatchResponseType,
|
|
|
|
BackupListMediaResponseType,
|
|
|
|
} from '../../textsecure/WebAPI';
|
|
|
|
import type { BackupCredentials } from './credentials';
|
2024-05-20 19:29:20 +00:00
|
|
|
import { uploadFile } from '../../util/uploadAttachment';
|
2024-04-22 14:11:36 +00:00
|
|
|
|
|
|
|
export class BackupAPI {
|
2024-05-02 17:11:34 +00:00
|
|
|
private cachedBackupInfo: GetBackupInfoResponseType | undefined;
|
2024-04-22 14:11:36 +00:00
|
|
|
constructor(private credentials: BackupCredentials) {}
|
|
|
|
|
|
|
|
public async refresh(): Promise<void> {
|
|
|
|
// TODO: DESKTOP-6979
|
|
|
|
await this.server.refreshBackup(
|
|
|
|
await this.credentials.getHeadersForToday()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getInfo(): Promise<GetBackupInfoResponseType> {
|
2024-05-02 17:11:34 +00:00
|
|
|
const backupInfo = await this.server.getBackupInfo(
|
2024-04-22 14:11:36 +00:00
|
|
|
await this.credentials.getHeadersForToday()
|
|
|
|
);
|
2024-05-02 17:11:34 +00:00
|
|
|
this.cachedBackupInfo = backupInfo;
|
|
|
|
return backupInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async getCachedInfo(): Promise<GetBackupInfoResponseType> {
|
|
|
|
if (this.cachedBackupInfo) {
|
|
|
|
return this.cachedBackupInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.getInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getMediaDir(): Promise<string> {
|
|
|
|
return (await this.getCachedInfo()).mediaDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getBackupDir(): Promise<string> {
|
|
|
|
return (await this.getCachedInfo())?.backupDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup name will change whenever a new backup is created, so we don't want to cache
|
|
|
|
// it
|
|
|
|
public async getBackupName(): Promise<string> {
|
|
|
|
return (await this.getInfo()).backupName;
|
2024-04-22 14:11:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 17:04:50 +00:00
|
|
|
public async upload(filePath: string, fileSize: number): Promise<void> {
|
|
|
|
const form = await this.server.getBackupUploadForm(
|
|
|
|
await this.credentials.getHeadersForToday()
|
|
|
|
);
|
|
|
|
|
2024-05-20 19:29:20 +00:00
|
|
|
await uploadFile({
|
|
|
|
absoluteCiphertextPath: filePath,
|
|
|
|
ciphertextFileSize: fileSize,
|
|
|
|
uploadForm: form,
|
2024-05-06 16:03:27 +00:00
|
|
|
});
|
2024-04-22 14:11:36 +00:00
|
|
|
}
|
|
|
|
|
2024-05-14 17:04:50 +00:00
|
|
|
public async getMediaUploadForm(): Promise<AttachmentV3ResponseType> {
|
2024-04-22 14:11:36 +00:00
|
|
|
return this.server.getBackupMediaUploadForm(
|
|
|
|
await this.credentials.getHeadersForToday()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async backupMediaBatch(
|
|
|
|
items: ReadonlyArray<BackupMediaItemType>
|
|
|
|
): Promise<BackupMediaBatchResponseType> {
|
|
|
|
return this.server.backupMediaBatch({
|
|
|
|
headers: await this.credentials.getHeadersForToday(),
|
|
|
|
items,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async listMedia({
|
|
|
|
cursor,
|
|
|
|
limit,
|
|
|
|
}: {
|
|
|
|
cursor?: string;
|
|
|
|
limit: number;
|
|
|
|
}): Promise<BackupListMediaResponseType> {
|
|
|
|
return this.server.backupListMedia({
|
|
|
|
headers: await this.credentials.getHeadersForToday(),
|
|
|
|
cursor,
|
|
|
|
limit,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-05-02 17:11:34 +00:00
|
|
|
public clearCache(): void {
|
|
|
|
this.cachedBackupInfo = undefined;
|
|
|
|
}
|
|
|
|
|
2024-04-22 14:11:36 +00:00
|
|
|
private get server(): WebAPIType {
|
|
|
|
const { server } = window.textsecure;
|
|
|
|
strictAssert(server, 'server not available');
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
}
|