@@ -4,6 +4,10 @@ import * as nacl from "./naclWorker/nacl"
44import { bytesToHex , ConsoleLogger , Logger , prefetch } from "./common" ;
55import { tick } from "svelte" ;
66
7+ // Before sending files larger than this, we should check whether they exist:
8+ const SMALL_FILE_THRESHOLD = 1024 * 128
9+
10+
711// Encapsulates communication with the server(s).
812export class Client {
913
@@ -96,7 +100,13 @@ export class Client {
96100 return response
97101 }
98102
99- async putAttachment ( userID : UserID , signature : Signature , fileName : string , blob : Blob ) : Promise < Response > {
103+ async putAttachment ( userID : UserID , signature : Signature , fileName : string , blob : Blob ) : Promise < void > {
104+ // If the file is already in the content store, we can save some bandwidth/time:
105+ if ( blob . size > SMALL_FILE_THRESHOLD ) {
106+ const { exists} = await this . headAttachment ( userID , signature , fileName )
107+ if ( exists ) return
108+ }
109+
100110 let url = this . attachmentURL ( userID , signature , fileName )
101111 let response : Response
102112 try {
@@ -109,11 +119,11 @@ export class Client {
109119 }
110120
111121 } catch ( e ) {
112- console . error ( "PUT exception:" , e )
122+ const { exists} = await this . headAttachment ( userID , signature , fileName )
123+ if ( exists ) return // Someone beat us to the upload, everything's OK.
124+ // else:
113125 throw e
114126 }
115-
116- return response
117127 }
118128
119129 private attachmentURL ( userID : UserID , signature : Signature , fileName : string ) {
@@ -339,7 +349,7 @@ export type AttachmentMeta = {
339349
340350export type GetItemOptions = {
341351 // When syncing items from one server to another, the receiving server MUST
342- // perform the verificiation , so verifying in the client is redundant and slow.
352+ // perform the verification , so verifying in the client is redundant and slow.
343353 // Set this flag to skip it.
344354 skipSignatureCheck ?: boolean
345355}
0 commit comments