@overmap-ai/core 1.0.46-project-attachments.6 → 1.0.46-project-attachments.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4799,6 +4799,67 @@ var __publicField = (obj, key, value) => {
4799
4799
  const promise = performRequest2();
4800
4800
  return [offlineAttachment, promise];
4801
4801
  }
4802
+ async replaceProjectAttachmentFile(attachmentId, newFile) {
4803
+ const { store } = this.client;
4804
+ const attachment = store.getState().projectReducer.attachments[attachmentId];
4805
+ if (!attachment)
4806
+ throw new Error(`Attachment ${attachmentId} not found`);
4807
+ let oldFile = void 0;
4808
+ const newSha1 = await hashFile(newFile);
4809
+ const performRequest2 = async () => {
4810
+ oldFile = await this.client.files.fetchCache(attachment.file_sha1);
4811
+ if (!oldFile) {
4812
+ console.error(`Failed to fetch old file from cache for sha1 ${attachment.file_sha1}.`);
4813
+ }
4814
+ if (!newFile.objectURL) {
4815
+ throw new Error(`newFile["objectURL"] is unexpectedly ${newFile.objectURL}`);
4816
+ }
4817
+ store.dispatch(
4818
+ updateProjectAttachment({
4819
+ ...attachment,
4820
+ file_sha1: newSha1,
4821
+ file: URL.createObjectURL(newFile)
4822
+ })
4823
+ );
4824
+ await this.client.files.addCache(newFile, newSha1);
4825
+ const [fileProps] = await this.client.files.uploadFileToS3(newSha1).catch((e) => {
4826
+ store.dispatch(updateProjectAttachment(attachment));
4827
+ throw e;
4828
+ });
4829
+ const promise2 = this.enqueueRequest({
4830
+ description: "Edit attachment",
4831
+ method: HttpMethod.PATCH,
4832
+ url: `/attachments/projects/${attachment.offline_id}/`,
4833
+ isResponseBlob: false,
4834
+ payload: fileProps,
4835
+ blockers: [attachmentId, newSha1],
4836
+ blocks: [attachmentId, newSha1]
4837
+ });
4838
+ try {
4839
+ const result = await promise2;
4840
+ void this.client.files.removeCache(attachment.file_sha1);
4841
+ return result;
4842
+ } catch (e) {
4843
+ if (oldFile) {
4844
+ store.dispatch(
4845
+ updateProjectAttachment({
4846
+ ...attachment,
4847
+ file_sha1: attachment.file_sha1,
4848
+ file: URL.createObjectURL(oldFile)
4849
+ })
4850
+ );
4851
+ }
4852
+ throw e;
4853
+ }
4854
+ };
4855
+ const offlineAttachment = {
4856
+ ...attachment,
4857
+ file_sha1: newSha1,
4858
+ file: URL.createObjectURL(newFile)
4859
+ };
4860
+ const promise = performRequest2();
4861
+ return [offlineAttachment, promise];
4862
+ }
4802
4863
  /**
4803
4864
  * Deletes an attachment and associated data in the cloud, in the Redux store and the cache.
4804
4865
  * @param issueAttachmentId