@@ -144,18 +144,65 @@ export default function RoleFitForm() {
144144 } ;
145145 } , [ DIRECTUS_URL ] ) ;
146146
147- /** Upload CV file to Directus */
147+ /** Compute SHA-256 (hex) of a File/Blob */
148+ async function sha256OfFile ( file : Blob ) : Promise < string > {
149+ const buf = await file . arrayBuffer ( ) ;
150+ const hashBuf = await crypto . subtle . digest ( "SHA-256" , buf ) ;
151+ const bytes = new Uint8Array ( hashBuf ) ;
152+ let hex = "" ;
153+ for ( let i = 0 ; i < bytes . length ; i ++ ) {
154+ const h = bytes [ i ] . toString ( 16 ) . padStart ( 2 , "0" ) ;
155+ hex += h ;
156+ }
157+ return hex ;
158+ }
159+
160+ /** Try to find an existing file by SHA in your file_checksum collection */
161+ async function findFileIdBySha ( sha : string ) : Promise < string | null > {
162+ const url = new URL ( `${ DIRECTUS_URL } /items/file_checksum` ) ;
163+ console . log ( url ) ;
164+ url . searchParams . set ( "filter[sha256][_eq]" , sha ) ;
165+ // return only the related file id to keep payload small
166+ url . searchParams . set ( "fields" , "file" ) ;
167+ url . searchParams . set ( "limit" , "1" ) ;
168+
169+ const res = await fetch ( url . toString ( ) , {
170+ headers : { Authorization : `Bearer ${ EXTERNAL . directus_key } ` } ,
171+ } ) ;
172+ const js = await res . json ( ) ;
173+ if ( ! res . ok ) throw new Error ( js ?. errors ?. [ 0 ] ?. message || "Checksum lookup failed" ) ;
174+
175+ const item = js ?. data ?. [ 0 ] ;
176+ return item ?. file ?? null ;
177+ }
178+
179+ /** Upload CV file to Directus (only if not already stored by sha) */
148180 const uploadFile = async ( file : File ) => {
181+ // 1) hash
182+ const sha = await sha256OfFile ( file ) ;
183+ console . log ( sha )
184+ // 2) check if we already have it
185+ const existingId = await findFileIdBySha ( sha ) ;
186+ console . log ( "existingId: " + existingId )
187+
188+ if ( existingId ) return existingId ;
189+ console . log ( "no same sha file found" )
190+
191+ // 3) upload
149192 const fd = new FormData ( ) ;
150193 fd . append ( "file" , file , file . name || "cv.pdf" ) ;
151- const res = await fetch ( `${ DIRECTUS_URL } /files` , {
194+ const uploadRes = await fetch ( `${ DIRECTUS_URL } /files` , {
152195 method : "POST" ,
153196 headers : { Authorization : `Bearer ${ EXTERNAL . directus_key } ` } ,
154197 body : fd ,
155198 } ) ;
156- const js = await res . json ( ) ;
157- if ( ! res . ok ) throw new Error ( js ?. errors ?. [ 0 ] ?. message || "File upload failed" ) ;
158- return js . data . id as string ;
199+ const uploadJs = await uploadRes . json ( ) ;
200+ if ( ! uploadRes . ok ) {
201+ throw new Error ( uploadJs ?. errors ?. [ 0 ] ?. message || "File upload failed" ) ;
202+ }
203+ const fileId = uploadJs . data . id as string ;
204+
205+ return fileId ;
159206 } ;
160207
161208 /** Create submission */
0 commit comments