New attachment storage system
This commit is contained in:
parent
273e1ccb15
commit
28664a606f
161 changed files with 2418 additions and 1562 deletions
57
ts/util/getLocalAttachmentUrl.ts
Normal file
57
ts/util/getLocalAttachmentUrl.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import type { AttachmentType } from '../types/Attachment';
|
||||
import { strictAssert } from './assert';
|
||||
|
||||
export enum AttachmentDisposition {
|
||||
Attachment = 'attachment',
|
||||
Temporary = 'temporary',
|
||||
Draft = 'draft',
|
||||
Sticker = 'sticker',
|
||||
AvatarData = 'avatarData',
|
||||
}
|
||||
|
||||
export type GetLocalAttachmentUrlOptionsType = Readonly<{
|
||||
disposition?: AttachmentDisposition;
|
||||
}>;
|
||||
|
||||
export function getLocalAttachmentUrl(
|
||||
attachment: Partial<
|
||||
Pick<
|
||||
AttachmentType,
|
||||
'version' | 'path' | 'localKey' | 'size' | 'contentType'
|
||||
>
|
||||
>,
|
||||
{
|
||||
disposition = AttachmentDisposition.Attachment,
|
||||
}: GetLocalAttachmentUrlOptionsType = {}
|
||||
): string {
|
||||
strictAssert(attachment.path != null, 'Attachment must be downloaded first');
|
||||
|
||||
// Fix Windows paths
|
||||
const path = attachment.path.replace(/\\/g, '/');
|
||||
|
||||
let url: URL;
|
||||
if (attachment.version !== 2) {
|
||||
url = new URL(`attachment://v1/${path}`);
|
||||
} else {
|
||||
url = new URL(`attachment://v${attachment.version}/${path}`);
|
||||
if (attachment.localKey != null) {
|
||||
url.searchParams.set('key', attachment.localKey);
|
||||
}
|
||||
}
|
||||
|
||||
if (attachment.size != null) {
|
||||
url.searchParams.set('size', attachment.size.toString());
|
||||
}
|
||||
|
||||
if (attachment.contentType != null) {
|
||||
url.searchParams.set('contentType', attachment.contentType);
|
||||
}
|
||||
|
||||
if (disposition !== AttachmentDisposition.Attachment) {
|
||||
url.searchParams.set('disposition', disposition);
|
||||
}
|
||||
return url.toString();
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue