2024-04-16 00:11:48 +00:00
|
|
|
// Copyright 2024 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
import { omit } from 'lodash';
|
|
|
|
|
|
|
|
import * as durations from '../util/durations';
|
|
|
|
import * as log from '../logging/log';
|
|
|
|
import {
|
|
|
|
type AttachmentDownloadJobTypeType,
|
|
|
|
type AttachmentDownloadJobType,
|
2024-05-29 23:46:43 +00:00
|
|
|
type CoreAttachmentDownloadJobType,
|
|
|
|
coreAttachmentDownloadJobSchema,
|
2024-04-16 00:11:48 +00:00
|
|
|
} from '../types/AttachmentDownload';
|
|
|
|
import {
|
2024-06-14 17:17:04 +00:00
|
|
|
AttachmentPermanentlyUndownloadableError,
|
2024-07-16 20:39:56 +00:00
|
|
|
downloadAttachment as downloadAttachmentUtil,
|
2024-04-16 00:11:48 +00:00
|
|
|
} from '../util/downloadAttachment';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataWriter } from '../sql/Client';
|
2024-04-16 00:11:48 +00:00
|
|
|
import { getValue } from '../RemoteConfig';
|
|
|
|
|
|
|
|
import { isInCall as isInCallSelector } from '../state/selectors/calling';
|
2024-07-16 20:39:56 +00:00
|
|
|
import {
|
|
|
|
AttachmentSizeError,
|
|
|
|
type AttachmentType,
|
|
|
|
AttachmentVariant,
|
2024-10-24 20:21:02 +00:00
|
|
|
mightBeOnBackupTier,
|
2024-07-16 20:39:56 +00:00
|
|
|
} from '../types/Attachment';
|
2024-04-16 00:11:48 +00:00
|
|
|
import { __DEPRECATED$getMessageById } from '../messages/getMessageById';
|
|
|
|
import {
|
|
|
|
KIBIBYTE,
|
|
|
|
getMaximumIncomingAttachmentSizeInKb,
|
|
|
|
getMaximumIncomingTextAttachmentSizeInKb,
|
|
|
|
} from '../types/AttachmentSize';
|
|
|
|
import { addAttachmentToMessage } from '../messageModifiers/AttachmentDownloads';
|
|
|
|
import * as Errors from '../types/errors';
|
|
|
|
import { redactGenericText } from '../util/privacy';
|
2024-05-29 23:46:43 +00:00
|
|
|
import {
|
|
|
|
JobManager,
|
|
|
|
type JobManagerParamsType,
|
|
|
|
type JobManagerJobResultType,
|
|
|
|
} from './JobManager';
|
2024-07-16 20:39:56 +00:00
|
|
|
import {
|
|
|
|
isImageTypeSupported,
|
|
|
|
isVideoTypeSupported,
|
|
|
|
} from '../util/GoogleChrome';
|
|
|
|
import type { MIMEType } from '../types/MIME';
|
2024-09-16 19:38:12 +00:00
|
|
|
import { AttachmentDownloadSource } from '../sql/Interface';
|
2024-09-03 22:00:51 +00:00
|
|
|
import { drop } from '../util/drop';
|
|
|
|
import { getAttachmentCiphertextLength } from '../AttachmentCrypto';
|
2024-10-02 19:03:10 +00:00
|
|
|
import { safeParsePartial } from '../util/schemas';
|
2024-10-28 22:25:15 +00:00
|
|
|
import { createBatcher } from '../util/batcher';
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
export enum AttachmentDownloadUrgency {
|
|
|
|
IMMEDIATE = 'immediate',
|
|
|
|
STANDARD = 'standard',
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type for adding a new job
|
|
|
|
export type NewAttachmentDownloadJobType = {
|
|
|
|
attachment: AttachmentType;
|
|
|
|
messageId: string;
|
|
|
|
receivedAt: number;
|
|
|
|
sentAt: number;
|
|
|
|
attachmentType: AttachmentDownloadJobTypeType;
|
2024-09-03 22:00:51 +00:00
|
|
|
source: AttachmentDownloadSource;
|
2024-04-16 00:11:48 +00:00
|
|
|
urgency?: AttachmentDownloadUrgency;
|
|
|
|
};
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
const MAX_CONCURRENT_JOBS = 3;
|
|
|
|
|
|
|
|
const DEFAULT_RETRY_CONFIG = {
|
|
|
|
maxAttempts: 5,
|
|
|
|
backoffConfig: {
|
|
|
|
// 30 seconds, 5 minutes, 50 minutes, (max) 6 hrs
|
|
|
|
multiplier: 10,
|
|
|
|
firstBackoffs: [30 * durations.SECOND],
|
|
|
|
maxBackoffTime: 6 * durations.HOUR,
|
2024-04-16 00:11:48 +00:00
|
|
|
},
|
|
|
|
};
|
2024-09-03 22:00:51 +00:00
|
|
|
const BACKUP_RETRY_CONFIG = {
|
|
|
|
...DEFAULT_RETRY_CONFIG,
|
|
|
|
maxAttempts: Infinity,
|
|
|
|
};
|
2024-05-29 23:46:43 +00:00
|
|
|
type AttachmentDownloadManagerParamsType = Omit<
|
|
|
|
JobManagerParamsType<CoreAttachmentDownloadJobType>,
|
2024-07-16 20:39:56 +00:00
|
|
|
'getNextJobs' | 'runJob'
|
2024-05-29 23:46:43 +00:00
|
|
|
> & {
|
2024-04-16 00:11:48 +00:00
|
|
|
getNextJobs: (options: {
|
|
|
|
limit: number;
|
|
|
|
prioritizeMessageIds?: Array<string>;
|
2024-09-16 19:38:12 +00:00
|
|
|
sources?: Array<AttachmentDownloadSource>;
|
2024-04-16 00:11:48 +00:00
|
|
|
timestamp?: number;
|
|
|
|
}) => Promise<Array<AttachmentDownloadJobType>>;
|
2024-07-16 20:39:56 +00:00
|
|
|
runDownloadAttachmentJob: (args: {
|
|
|
|
job: AttachmentDownloadJobType;
|
|
|
|
isLastAttempt: boolean;
|
|
|
|
options?: { isForCurrentlyVisibleMessage: boolean };
|
2024-10-04 05:52:29 +00:00
|
|
|
dependencies?: DependenciesType;
|
2024-07-16 20:39:56 +00:00
|
|
|
}) => Promise<JobManagerJobResultType<CoreAttachmentDownloadJobType>>;
|
2024-04-16 00:11:48 +00:00
|
|
|
};
|
2024-05-29 23:46:43 +00:00
|
|
|
|
|
|
|
function getJobId(job: CoreAttachmentDownloadJobType): string {
|
|
|
|
const { messageId, attachmentType, digest } = job;
|
|
|
|
return `${messageId}.${attachmentType}.${digest}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJobIdForLogging(job: CoreAttachmentDownloadJobType): string {
|
|
|
|
const { sentAt, attachmentType, digest } = job;
|
|
|
|
const redactedDigest = redactGenericText(digest);
|
|
|
|
return `${sentAt}.${attachmentType}.${redactedDigest}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AttachmentDownloadManager extends JobManager<CoreAttachmentDownloadJobType> {
|
2024-07-16 20:39:56 +00:00
|
|
|
private visibleTimelineMessages: Set<string> = new Set();
|
2024-10-28 22:25:15 +00:00
|
|
|
private saveJobsBatcher = createBatcher<AttachmentDownloadJobType>({
|
|
|
|
name: 'saveAttachmentDownloadJobs',
|
|
|
|
wait: 150,
|
|
|
|
maxSize: 1000,
|
|
|
|
processBatch: async jobs => {
|
|
|
|
await DataWriter.saveAttachmentDownloadJobs(jobs);
|
|
|
|
drop(this.maybeStartJobs());
|
|
|
|
},
|
|
|
|
});
|
2024-05-29 23:46:43 +00:00
|
|
|
private static _instance: AttachmentDownloadManager | undefined;
|
|
|
|
override logPrefix = 'AttachmentDownloadManager';
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
static defaultParams: AttachmentDownloadManagerParamsType = {
|
2024-07-22 18:16:33 +00:00
|
|
|
markAllJobsInactive: DataWriter.resetAttachmentDownloadActive,
|
2024-10-28 22:25:15 +00:00
|
|
|
saveJob: async (job, options) => {
|
|
|
|
if (options?.allowBatching) {
|
|
|
|
AttachmentDownloadManager._instance?.saveJobsBatcher.add(job);
|
|
|
|
} else {
|
|
|
|
await DataWriter.saveAttachmentDownloadJob(job);
|
|
|
|
}
|
|
|
|
},
|
2024-07-22 18:16:33 +00:00
|
|
|
removeJob: DataWriter.removeAttachmentDownloadJob,
|
|
|
|
getNextJobs: DataWriter.getNextAttachmentDownloadJobs,
|
2024-07-16 20:39:56 +00:00
|
|
|
runDownloadAttachmentJob,
|
2024-05-29 23:46:43 +00:00
|
|
|
shouldHoldOffOnStartingQueuedJobs: () => {
|
2024-04-16 00:11:48 +00:00
|
|
|
const reduxState = window.reduxStore?.getState();
|
|
|
|
if (reduxState) {
|
|
|
|
return isInCallSelector(reduxState);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2024-05-29 23:46:43 +00:00
|
|
|
getJobId,
|
|
|
|
getJobIdForLogging,
|
2024-09-03 22:00:51 +00:00
|
|
|
getRetryConfig: job =>
|
|
|
|
job.attachment.backupLocator?.mediaName
|
|
|
|
? BACKUP_RETRY_CONFIG
|
|
|
|
: DEFAULT_RETRY_CONFIG,
|
2024-05-29 23:46:43 +00:00
|
|
|
maxConcurrentJobs: MAX_CONCURRENT_JOBS,
|
2024-04-16 00:11:48 +00:00
|
|
|
};
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
constructor(params: AttachmentDownloadManagerParamsType) {
|
|
|
|
super({
|
|
|
|
...params,
|
|
|
|
getNextJobs: ({ limit }) => {
|
|
|
|
return params.getNextJobs({
|
|
|
|
limit,
|
2024-07-16 20:39:56 +00:00
|
|
|
prioritizeMessageIds: [...this.visibleTimelineMessages],
|
2024-09-16 19:38:12 +00:00
|
|
|
sources: window.storage.get('backupMediaDownloadPaused')
|
|
|
|
? [AttachmentDownloadSource.STANDARD]
|
|
|
|
: undefined,
|
2024-05-29 23:46:43 +00:00
|
|
|
timestamp: Date.now(),
|
|
|
|
});
|
|
|
|
},
|
2024-07-16 20:39:56 +00:00
|
|
|
runJob: (job: AttachmentDownloadJobType, isLastAttempt: boolean) => {
|
|
|
|
const isForCurrentlyVisibleMessage = this.visibleTimelineMessages.has(
|
|
|
|
job.messageId
|
|
|
|
);
|
|
|
|
return params.runDownloadAttachmentJob({
|
|
|
|
job,
|
|
|
|
isLastAttempt,
|
|
|
|
options: {
|
|
|
|
isForCurrentlyVisibleMessage,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2024-05-29 23:46:43 +00:00
|
|
|
});
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-29 23:46:43 +00:00
|
|
|
// @ts-expect-error we are overriding the return type of JobManager's addJob
|
|
|
|
override async addJob(
|
2024-04-16 00:11:48 +00:00
|
|
|
newJobData: NewAttachmentDownloadJobType
|
|
|
|
): Promise<AttachmentType> {
|
|
|
|
const {
|
|
|
|
attachment,
|
|
|
|
messageId,
|
|
|
|
attachmentType,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
2024-09-03 22:00:51 +00:00
|
|
|
source,
|
2024-04-16 00:11:48 +00:00
|
|
|
urgency = AttachmentDownloadUrgency.STANDARD,
|
|
|
|
} = newJobData;
|
2024-10-02 19:03:10 +00:00
|
|
|
const parseResult = safeParsePartial(coreAttachmentDownloadJobSchema, {
|
2024-04-16 00:11:48 +00:00
|
|
|
messageId,
|
|
|
|
receivedAt,
|
|
|
|
sentAt,
|
|
|
|
attachmentType,
|
|
|
|
digest: attachment.digest,
|
|
|
|
contentType: attachment.contentType,
|
|
|
|
size: attachment.size,
|
2024-09-03 22:00:51 +00:00
|
|
|
ciphertextSize: getAttachmentCiphertextLength(attachment.size),
|
2024-04-16 00:11:48 +00:00
|
|
|
attachment,
|
2024-10-24 20:21:02 +00:00
|
|
|
// If the attachment does not have a backupLocator, we don't want to store it as a
|
|
|
|
// "backup import" attachment, since it's really just a normal attachment that we'll
|
|
|
|
// try to download from the transit tier (or it's an invalid attachment, etc.). We
|
|
|
|
// may need to extend the attachment_downloads table in the future to better
|
|
|
|
// differentiate source vs. location.
|
|
|
|
source: mightBeOnBackupTier(attachment)
|
|
|
|
? source
|
|
|
|
: AttachmentDownloadSource.STANDARD,
|
2024-04-16 00:11:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!parseResult.success) {
|
|
|
|
log.error(
|
|
|
|
`AttachmentDownloadManager/addJob(${sentAt}.${attachmentType}): invalid data`,
|
|
|
|
parseResult.error
|
|
|
|
);
|
|
|
|
return attachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newJob = parseResult.data;
|
|
|
|
|
2024-08-08 20:32:11 +00:00
|
|
|
await this._addJob(newJob, {
|
2024-05-29 23:46:43 +00:00
|
|
|
forceStart: urgency === AttachmentDownloadUrgency.IMMEDIATE,
|
|
|
|
});
|
2024-04-16 00:11:48 +00:00
|
|
|
|
2024-08-08 20:32:11 +00:00
|
|
|
return attachment;
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateVisibleTimelineMessages(messageIds: Array<string>): void {
|
2024-07-16 20:39:56 +00:00
|
|
|
this.visibleTimelineMessages = new Set(messageIds);
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static get instance(): AttachmentDownloadManager {
|
|
|
|
if (!AttachmentDownloadManager._instance) {
|
2024-05-29 23:46:43 +00:00
|
|
|
AttachmentDownloadManager._instance = new AttachmentDownloadManager(
|
|
|
|
AttachmentDownloadManager.defaultParams
|
|
|
|
);
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
return AttachmentDownloadManager._instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async start(): Promise<void> {
|
2024-10-28 22:25:15 +00:00
|
|
|
await AttachmentDownloadManager.saveBatchedJobs();
|
2024-04-16 00:11:48 +00:00
|
|
|
await AttachmentDownloadManager.instance.start();
|
|
|
|
}
|
|
|
|
|
2024-10-28 22:25:15 +00:00
|
|
|
static async saveBatchedJobs(): Promise<void> {
|
|
|
|
await AttachmentDownloadManager.instance.saveJobsBatcher.flushAndWait();
|
|
|
|
}
|
|
|
|
|
2024-04-16 00:11:48 +00:00
|
|
|
static async stop(): Promise<void> {
|
|
|
|
return AttachmentDownloadManager._instance?.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
static async addJob(
|
|
|
|
newJob: NewAttachmentDownloadJobType
|
|
|
|
): Promise<AttachmentType> {
|
|
|
|
return AttachmentDownloadManager.instance.addJob(newJob);
|
|
|
|
}
|
|
|
|
|
|
|
|
static updateVisibleTimelineMessages(messageIds: Array<string>): void {
|
|
|
|
AttachmentDownloadManager.instance.updateVisibleTimelineMessages(
|
|
|
|
messageIds
|
|
|
|
);
|
|
|
|
}
|
2024-10-24 20:21:02 +00:00
|
|
|
|
|
|
|
static async waitForIdle(callback?: VoidFunction): Promise<void> {
|
|
|
|
await AttachmentDownloadManager.instance.waitForIdle();
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
2024-10-04 05:52:29 +00:00
|
|
|
|
|
|
|
type DependenciesType = {
|
|
|
|
downloadAttachment: typeof downloadAttachmentUtil;
|
|
|
|
processNewAttachment: typeof window.Signal.Migrations.processNewAttachment;
|
|
|
|
};
|
2024-07-16 20:39:56 +00:00
|
|
|
async function runDownloadAttachmentJob({
|
|
|
|
job,
|
|
|
|
isLastAttempt,
|
|
|
|
options,
|
2024-10-04 05:52:29 +00:00
|
|
|
dependencies = {
|
|
|
|
downloadAttachment: downloadAttachmentUtil,
|
|
|
|
processNewAttachment: window.Signal.Migrations.processNewAttachment,
|
|
|
|
},
|
2024-07-16 20:39:56 +00:00
|
|
|
}: {
|
|
|
|
job: AttachmentDownloadJobType;
|
|
|
|
isLastAttempt: boolean;
|
|
|
|
options?: { isForCurrentlyVisibleMessage: boolean };
|
2024-10-04 05:52:29 +00:00
|
|
|
dependencies?: DependenciesType;
|
2024-07-16 20:39:56 +00:00
|
|
|
}): Promise<JobManagerJobResultType<CoreAttachmentDownloadJobType>> {
|
2024-04-16 00:11:48 +00:00
|
|
|
const jobIdForLogging = getJobIdForLogging(job);
|
2024-04-19 17:09:51 +00:00
|
|
|
const logId = `AttachmentDownloadManager/runDownloadAttachmentJob/${jobIdForLogging}`;
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
const message = await __DEPRECATED$getMessageById(job.messageId);
|
|
|
|
|
|
|
|
if (!message) {
|
|
|
|
log.error(`${logId} message not found`);
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
log.info(`${logId}: Starting job`);
|
2024-07-16 20:39:56 +00:00
|
|
|
|
|
|
|
const result = await runDownloadAttachmentJobInner({
|
|
|
|
job,
|
|
|
|
isForCurrentlyVisibleMessage:
|
|
|
|
options?.isForCurrentlyVisibleMessage ?? false,
|
|
|
|
dependencies,
|
|
|
|
});
|
|
|
|
|
2024-09-03 22:00:51 +00:00
|
|
|
if (result.downloadedVariant === AttachmentVariant.ThumbnailFromBackup) {
|
2024-07-16 20:39:56 +00:00
|
|
|
return {
|
|
|
|
status: 'finished',
|
|
|
|
newJob: { ...job, attachment: result.attachmentWithThumbnail },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-10-24 20:21:02 +00:00
|
|
|
if (mightBeOnBackupTier(job.attachment)) {
|
2024-09-03 22:00:51 +00:00
|
|
|
const currentDownloadedSize =
|
2024-09-16 19:38:12 +00:00
|
|
|
window.storage.get('backupMediaDownloadCompletedBytes') ?? 0;
|
2024-09-03 22:00:51 +00:00
|
|
|
drop(
|
|
|
|
window.storage.put(
|
2024-09-16 19:38:12 +00:00
|
|
|
'backupMediaDownloadCompletedBytes',
|
2024-09-03 22:00:51 +00:00
|
|
|
currentDownloadedSize + job.ciphertextSize
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-16 20:39:56 +00:00
|
|
|
return {
|
|
|
|
status: 'finished',
|
|
|
|
};
|
2024-04-16 00:11:48 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
`${logId}: Failed to download attachment, attempt ${job.attempts}:`,
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (error instanceof AttachmentSizeError) {
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
_markAttachmentAsTooBig(job.attachment),
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
2024-06-14 17:17:04 +00:00
|
|
|
if (error instanceof AttachmentPermanentlyUndownloadableError) {
|
2024-04-16 00:11:48 +00:00
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
_markAttachmentAsPermanentlyErrored(job.attachment),
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isLastAttempt) {
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
_markAttachmentAsTransientlyErrored(job.attachment),
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
return { status: 'finished' };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove `pending` flag from the attachment and retry later
|
|
|
|
await addAttachmentToMessage(
|
2024-05-15 14:19:55 +00:00
|
|
|
message.id,
|
2024-04-16 00:11:48 +00:00
|
|
|
{
|
|
|
|
...job.attachment,
|
|
|
|
pending: false,
|
|
|
|
},
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ type: job.attachmentType }
|
|
|
|
);
|
|
|
|
return { status: 'retry' };
|
|
|
|
} finally {
|
|
|
|
// This will fail if the message has been deleted before the download finished, which
|
|
|
|
// is good
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.saveMessage(message.attributes, {
|
2024-04-16 00:11:48 +00:00
|
|
|
ourAci: window.textsecure.storage.user.getCheckedAci(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 20:39:56 +00:00
|
|
|
type DownloadAttachmentResultType =
|
2024-09-03 22:00:51 +00:00
|
|
|
| { downloadedVariant: AttachmentVariant.Default }
|
2024-07-16 20:39:56 +00:00
|
|
|
| {
|
2024-09-03 22:00:51 +00:00
|
|
|
downloadedVariant: AttachmentVariant.ThumbnailFromBackup;
|
2024-07-16 20:39:56 +00:00
|
|
|
attachmentWithThumbnail: AttachmentType;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function runDownloadAttachmentJobInner({
|
|
|
|
job,
|
|
|
|
isForCurrentlyVisibleMessage,
|
|
|
|
dependencies,
|
|
|
|
}: {
|
|
|
|
job: AttachmentDownloadJobType;
|
|
|
|
isForCurrentlyVisibleMessage: boolean;
|
2024-10-04 05:52:29 +00:00
|
|
|
dependencies: DependenciesType;
|
2024-07-16 20:39:56 +00:00
|
|
|
}): Promise<DownloadAttachmentResultType> {
|
|
|
|
const { messageId, attachment, attachmentType } = job;
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
const jobIdForLogging = getJobIdForLogging(job);
|
2024-07-16 20:39:56 +00:00
|
|
|
let logId = `AttachmentDownloadManager/runDownloadJobInner(${jobIdForLogging})`;
|
2024-04-16 00:11:48 +00:00
|
|
|
|
|
|
|
if (!job || !attachment || !messageId) {
|
|
|
|
throw new Error(`${logId}: Key information required for job was missing.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxInKib = getMaximumIncomingAttachmentSizeInKb(getValue);
|
|
|
|
const maxTextAttachmentSizeInKib =
|
|
|
|
getMaximumIncomingTextAttachmentSizeInKb(getValue);
|
|
|
|
|
|
|
|
const { size } = attachment;
|
|
|
|
const sizeInKib = size / KIBIBYTE;
|
|
|
|
|
|
|
|
if (!Number.isFinite(size) || size < 0 || sizeInKib > maxInKib) {
|
|
|
|
throw new AttachmentSizeError(
|
|
|
|
`${logId}: Attachment was ${sizeInKib}kib, max is ${maxInKib}kib`
|
|
|
|
);
|
|
|
|
}
|
2024-07-16 20:39:56 +00:00
|
|
|
if (
|
|
|
|
attachmentType === 'long-message' &&
|
|
|
|
sizeInKib > maxTextAttachmentSizeInKib
|
|
|
|
) {
|
2024-04-16 00:11:48 +00:00
|
|
|
throw new AttachmentSizeError(
|
|
|
|
`${logId}: Text attachment was ${sizeInKib}kib, max is ${maxTextAttachmentSizeInKib}kib`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-16 20:39:56 +00:00
|
|
|
const preferBackupThumbnail =
|
|
|
|
isForCurrentlyVisibleMessage &&
|
|
|
|
mightHaveThumbnailOnBackupTier(job.attachment) &&
|
|
|
|
// TODO (DESKTOP-7204): check if thumbnail exists on attachment, not on job
|
|
|
|
!job.attachment.thumbnailFromBackup;
|
|
|
|
|
|
|
|
if (preferBackupThumbnail) {
|
|
|
|
logId += '.preferringBackupThumbnail';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preferBackupThumbnail) {
|
|
|
|
try {
|
|
|
|
const attachmentWithThumbnail = await downloadBackupThumbnail({
|
|
|
|
attachment,
|
|
|
|
dependencies,
|
|
|
|
});
|
|
|
|
await addAttachmentToMessage(messageId, attachmentWithThumbnail, logId, {
|
|
|
|
type: attachmentType,
|
|
|
|
});
|
|
|
|
return {
|
2024-09-03 22:00:51 +00:00
|
|
|
downloadedVariant: AttachmentVariant.ThumbnailFromBackup,
|
2024-07-16 20:39:56 +00:00
|
|
|
attachmentWithThumbnail,
|
|
|
|
};
|
|
|
|
} catch (e) {
|
|
|
|
log.warn(`${logId}: error when trying to download thumbnail`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO (DESKTOP-7204): currently we only set pending state when downloading the
|
|
|
|
// full-size attachment
|
2024-04-16 00:11:48 +00:00
|
|
|
await addAttachmentToMessage(
|
2024-07-16 20:39:56 +00:00
|
|
|
messageId,
|
2024-04-16 00:11:48 +00:00
|
|
|
{ ...attachment, pending: true },
|
2024-04-19 17:09:51 +00:00
|
|
|
logId,
|
2024-07-16 20:39:56 +00:00
|
|
|
{ type: attachmentType }
|
2024-04-16 00:11:48 +00:00
|
|
|
);
|
|
|
|
|
2024-07-16 20:39:56 +00:00
|
|
|
try {
|
|
|
|
const downloaded = await dependencies.downloadAttachment({
|
|
|
|
attachment,
|
|
|
|
variant: AttachmentVariant.Default,
|
|
|
|
});
|
2024-04-16 00:11:48 +00:00
|
|
|
|
2024-10-04 05:52:29 +00:00
|
|
|
const upgradedAttachment = await dependencies.processNewAttachment({
|
|
|
|
...omit(attachment, ['error', 'pending', 'downloadPath']),
|
|
|
|
...downloaded,
|
|
|
|
});
|
2024-04-16 00:11:48 +00:00
|
|
|
|
2024-07-16 20:39:56 +00:00
|
|
|
await addAttachmentToMessage(messageId, upgradedAttachment, logId, {
|
|
|
|
type: attachmentType,
|
|
|
|
});
|
2024-09-03 22:00:51 +00:00
|
|
|
return { downloadedVariant: AttachmentVariant.Default };
|
2024-07-16 20:39:56 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (
|
|
|
|
!job.attachment.thumbnailFromBackup &&
|
|
|
|
mightHaveThumbnailOnBackupTier(attachment) &&
|
|
|
|
!preferBackupThumbnail
|
|
|
|
) {
|
|
|
|
log.error(
|
|
|
|
`${logId}: failed to download fullsize attachment, falling back to thumbnail`,
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
try {
|
2024-09-03 22:00:51 +00:00
|
|
|
const attachmentWithThumbnail = omit(
|
|
|
|
await downloadBackupThumbnail({
|
|
|
|
attachment,
|
|
|
|
dependencies,
|
|
|
|
}),
|
|
|
|
'pending'
|
|
|
|
);
|
2024-07-16 20:39:56 +00:00
|
|
|
await addAttachmentToMessage(
|
|
|
|
messageId,
|
2024-09-03 22:00:51 +00:00
|
|
|
attachmentWithThumbnail,
|
2024-07-16 20:39:56 +00:00
|
|
|
logId,
|
|
|
|
{
|
|
|
|
type: attachmentType,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return {
|
2024-09-03 22:00:51 +00:00
|
|
|
downloadedVariant: AttachmentVariant.ThumbnailFromBackup,
|
|
|
|
attachmentWithThumbnail,
|
2024-07-16 20:39:56 +00:00
|
|
|
};
|
|
|
|
} catch (thumbnailError) {
|
|
|
|
log.error(
|
|
|
|
`${logId}: fallback attempt to download thumbnail failed`,
|
|
|
|
Errors.toLogFormat(thumbnailError)
|
|
|
|
);
|
|
|
|
}
|
2024-04-18 20:49:12 +00:00
|
|
|
}
|
2024-07-16 20:39:56 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function downloadBackupThumbnail({
|
|
|
|
attachment,
|
|
|
|
dependencies,
|
|
|
|
}: {
|
|
|
|
attachment: AttachmentType;
|
|
|
|
dependencies: { downloadAttachment: typeof downloadAttachmentUtil };
|
|
|
|
}): Promise<AttachmentType> {
|
|
|
|
const downloadedThumbnail = await dependencies.downloadAttachment({
|
|
|
|
attachment,
|
|
|
|
variant: AttachmentVariant.ThumbnailFromBackup,
|
|
|
|
});
|
|
|
|
|
|
|
|
const attachmentWithThumbnail = {
|
|
|
|
...attachment,
|
|
|
|
thumbnailFromBackup: downloadedThumbnail,
|
|
|
|
};
|
|
|
|
|
|
|
|
return attachmentWithThumbnail;
|
2024-04-16 00:11:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function _markAttachmentAsTooBig(attachment: AttachmentType): AttachmentType {
|
|
|
|
return {
|
|
|
|
..._markAttachmentAsPermanentlyErrored(attachment),
|
|
|
|
wasTooBig: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function _markAttachmentAsPermanentlyErrored(
|
|
|
|
attachment: AttachmentType
|
|
|
|
): AttachmentType {
|
|
|
|
return { ...omit(attachment, ['key', 'id']), pending: false, error: true };
|
|
|
|
}
|
|
|
|
|
|
|
|
function _markAttachmentAsTransientlyErrored(
|
|
|
|
attachment: AttachmentType
|
|
|
|
): AttachmentType {
|
|
|
|
return { ...attachment, pending: false, error: true };
|
|
|
|
}
|
2024-07-16 20:39:56 +00:00
|
|
|
|
|
|
|
function mightHaveThumbnailOnBackupTier(
|
|
|
|
attachment: Pick<AttachmentType, 'backupLocator' | 'contentType'>
|
|
|
|
): boolean {
|
|
|
|
if (!attachment.backupLocator?.mediaName) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return canAttachmentHaveThumbnail(attachment.contentType);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function canAttachmentHaveThumbnail(contentType: MIMEType): boolean {
|
|
|
|
return isVideoTypeSupported(contentType) || isImageTypeSupported(contentType);
|
|
|
|
}
|