| 
									
										
										
										
											2024-01-02 15:14:11 -05:00
										 |  |  | // Copyright 2023 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { isFunction, isTypedArray, isUndefined, omit } from 'lodash'; | 
					
						
							| 
									
										
										
										
											2024-07-11 12:44:09 -07:00
										 |  |  | import type { | 
					
						
							|  |  |  |   AttachmentType, | 
					
						
							|  |  |  |   LocalAttachmentV2Type, | 
					
						
							|  |  |  | } from '../../types/Attachment'; | 
					
						
							| 
									
										
										
										
											2024-01-02 15:14:11 -05:00
										 |  |  | import type { LoggerType } from '../../types/Logging'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export async function migrateDataToFileSystem( | 
					
						
							|  |  |  |   attachment: AttachmentType, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     writeNewAttachmentData, | 
					
						
							|  |  |  |     logger, | 
					
						
							|  |  |  |   }: { | 
					
						
							| 
									
										
										
										
											2024-07-11 12:44:09 -07:00
										 |  |  |     writeNewAttachmentData: ( | 
					
						
							|  |  |  |       data: Uint8Array | 
					
						
							|  |  |  |     ) => Promise<LocalAttachmentV2Type>; | 
					
						
							| 
									
										
										
										
											2024-01-02 15:14:11 -05:00
										 |  |  |     logger: LoggerType; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | ): Promise<AttachmentType> { | 
					
						
							|  |  |  |   if (!isFunction(writeNewAttachmentData)) { | 
					
						
							|  |  |  |     throw new TypeError("'writeNewAttachmentData' must be a function"); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const { data } = attachment; | 
					
						
							|  |  |  |   const attachmentHasData = !isUndefined(data); | 
					
						
							|  |  |  |   const shouldSkipSchemaUpgrade = !attachmentHasData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (shouldSkipSchemaUpgrade) { | 
					
						
							|  |  |  |     return attachment; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // This attachment was already broken by a roundtrip to the database - repair it now
 | 
					
						
							|  |  |  |   if (!isTypedArray(data)) { | 
					
						
							|  |  |  |     logger.warn( | 
					
						
							|  |  |  |       'migrateDataToFileSystem: Attachment had non-array `data` field; deleting.' | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     return omit({ ...attachment }, ['data']); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 12:44:09 -07:00
										 |  |  |   const local = await writeNewAttachmentData(data); | 
					
						
							| 
									
										
										
										
											2024-01-02 15:14:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-11 12:44:09 -07:00
										 |  |  |   const attachmentWithoutData = omit({ ...attachment, ...local }, ['data']); | 
					
						
							| 
									
										
										
										
											2024-01-02 15:14:11 -05:00
										 |  |  |   return attachmentWithoutData; | 
					
						
							|  |  |  | } |