( attachment?: Proto.AttachmentPointer | null )
| 73 | ): ProcessedAttachment | undefined; |
| 74 | |
| 75 | export function processAttachment( |
| 76 | attachment?: Proto.AttachmentPointer | null |
| 77 | ): ProcessedAttachment | undefined { |
| 78 | if (attachment == null) { |
| 79 | return undefined; |
| 80 | } |
| 81 | |
| 82 | const { |
| 83 | cdnNumber, |
| 84 | attachmentIdentifier, |
| 85 | clientUuid, |
| 86 | key, |
| 87 | size, |
| 88 | contentType, |
| 89 | digest, |
| 90 | incrementalMac, |
| 91 | chunkSize, |
| 92 | fileName, |
| 93 | flags, |
| 94 | width, |
| 95 | height, |
| 96 | caption, |
| 97 | blurHash, |
| 98 | } = attachment; |
| 99 | |
| 100 | if (!isNumber(size)) { |
| 101 | throw new Error('Missing size on incoming attachment!'); |
| 102 | } |
| 103 | |
| 104 | let uploadTimestamp: number | undefined = |
| 105 | toNumber(attachment.uploadTimestamp) ?? 0; |
| 106 | |
| 107 | // Make sure uploadTimestamp is not set to an obviously wrong future value (we use |
| 108 | // uploadTimestamp to determine whether to re-use CDN pointers) |
| 109 | if (uploadTimestamp && uploadTimestamp > Date.now() + 12 * HOUR) { |
| 110 | log.warn('uploadTimestamp is in the future, dropping'); |
| 111 | uploadTimestamp = undefined; |
| 112 | } |
| 113 | |
| 114 | return { |
| 115 | cdnKey: attachmentIdentifier?.cdnKey, |
| 116 | cdnNumber: cdnNumber ?? 0, |
| 117 | chunkSize: chunkSize ?? 0, |
| 118 | fileName: fileName ?? '', |
| 119 | flags: flags ?? 0, |
| 120 | width: width ?? 0, |
| 121 | height: height ?? 0, |
| 122 | caption: caption ?? '', |
| 123 | blurHash: blurHash ?? '', |
| 124 | uploadTimestamp, |
| 125 | cdnId: |
| 126 | attachmentIdentifier?.cdnId === 0n |
| 127 | ? undefined |
| 128 | : attachmentIdentifier?.cdnId?.toString(), |
| 129 | clientUuid: Bytes.isNotEmpty(clientUuid) |
| 130 | ? bytesToUuid(clientUuid) |
| 131 | : undefined, |
| 132 | contentType: contentType |
no test coverage detected