@@ -878,21 +878,39 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
878
878
// upload a copy of the template tar as a file in the database
879
879
hashBytes := sha256 .Sum256 (tar )
880
880
hash := hex .EncodeToString (hashBytes [:])
881
- file , err = api . Database . InsertFile ( ctx , database. InsertFileParams {
882
- ID : uuid . New (),
881
+ // Check if the file already exists.
882
+ file , err := api . Database . GetFileByHashAndCreator ( ctx , database. GetFileByHashAndCreatorParams {
883
883
Hash : hash ,
884
884
CreatedBy : apiKey .UserID ,
885
- CreatedAt : database .Now (),
886
- Mimetype : tarMimeType ,
887
- Data : tar ,
888
885
})
889
886
if err != nil {
890
- httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
891
- Message : "Internal error creating file." ,
892
- Detail : err .Error (),
887
+ if ! errors .Is (err , sql .ErrNoRows ) {
888
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
889
+ Message : "Internal error fetching file." ,
890
+ Detail : err .Error (),
891
+ })
892
+ return
893
+ }
894
+
895
+ // If the example tar file doesn't exist, create it.
896
+ file , err = api .Database .InsertFile (ctx , database.InsertFileParams {
897
+ ID : uuid .New (),
898
+ Hash : hash ,
899
+ CreatedBy : apiKey .UserID ,
900
+ CreatedAt : database .Now (),
901
+ Mimetype : tarMimeType ,
902
+ Data : tar ,
893
903
})
894
- return
904
+ if err != nil {
905
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
906
+ Message : "Internal error creating file." ,
907
+ Detail : err .Error (),
908
+ })
909
+ return
910
+ }
895
911
}
912
+
913
+ req .FileID = file .ID
896
914
}
897
915
898
916
if req .FileID != uuid .Nil {
0 commit comments