2022-02-08 12:00:44 -06:00
package coderd
import (
2024-01-31 14:49:55 +01:00
"archive/tar"
"archive/zip"
"bytes"
2022-02-08 12:00:44 -06:00
"crypto/sha256"
2022-03-07 11:40:54 -06:00
"database/sql"
2022-02-08 12:00:44 -06:00
"encoding/hex"
2022-03-07 11:40:54 -06:00
"errors"
2022-02-08 12:00:44 -06:00
"fmt"
"io"
"net/http"
2022-03-07 11:40:54 -06:00
"github.com/go-chi/chi/v5"
2022-10-13 18:02:52 -05:00
"github.com/google/uuid"
2022-02-08 12:00:44 -06:00
2024-01-31 23:17:20 -06:00
"cdr.dev/slog"
2024-10-25 15:14:39 +01:00
"github.com/coder/coder/v2/archive"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/database"
2023-09-01 19:50:12 +03:00
"github.com/coder/coder/v2/coderd/database/dbtime"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
"github.com/coder/coder/v2/codersdk"
2022-02-08 12:00:44 -06:00
)
2022-12-09 14:29:50 -05:00
const (
2024-11-08 18:24:12 +11:00
tarMimeType = "application/x-tar"
zipMimeType = "application/zip"
windowsZipMimeType = "application/x-zip-compressed"
2024-01-31 14:49:55 +01:00
2024-10-25 15:14:39 +01:00
HTTPFileMaxBytes = 10 * ( 10 << 20 )
2022-12-09 14:29:50 -05:00
)
2023-01-03 19:21:10 +01:00
// @Summary Upload file
// @Description Swagger notice: Swagger 2.0 doesn't support file upload with a `content-type` different than `application/x-www-form-urlencoded`.
2023-01-13 12:27:21 +01:00
// @ID upload-file
2023-01-03 19:21:10 +01:00
// @Security CoderSessionToken
// @Produce json
// @Accept application/x-tar
// @Tags Files
2024-01-31 14:49:55 +01:00
// @Param Content-Type header string true "Content-Type must be `application/x-tar` or `application/zip`" default(application/x-tar)
2024-08-06 11:00:49 +04:00
// @Param file formData file true "File to be uploaded. If using tar format, file must conform to ustar (pax may cause problems)."
2023-01-03 19:21:10 +01:00
// @Success 201 {object} codersdk.UploadResponse
// @Router /files [post]
2022-05-25 22:14:08 -05:00
func ( api * API ) postFile ( rw http . ResponseWriter , r * http . Request ) {
2022-09-21 17:07:00 -05:00
ctx := r . Context ()
2022-02-08 12:00:44 -06:00
apiKey := httpmw . APIKey ( r )
2022-05-24 08:25:02 -05:00
2022-02-08 12:00:44 -06:00
contentType := r . Header . Get ( "Content-Type" )
switch contentType {
2024-11-08 18:24:12 +11:00
case tarMimeType , zipMimeType , windowsZipMimeType :
2022-02-08 12:00:44 -06:00
default :
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
2022-06-07 09:33:06 -05:00
Message : fmt . Sprintf ( "Unsupported content type header %q." , contentType ),
2022-02-08 12:00:44 -06:00
})
return
}
2024-10-25 15:14:39 +01:00
r . Body = http . MaxBytesReader ( rw , r . Body , HTTPFileMaxBytes )
2022-02-08 12:00:44 -06:00
data , err := io . ReadAll ( r . Body )
if err != nil {
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
2022-06-07 09:33:06 -05:00
Message : "Failed to read file from request." ,
2022-06-03 16:48:09 -05:00
Detail : err . Error (),
2022-02-08 12:00:44 -06:00
})
return
}
2024-01-31 14:49:55 +01:00
2024-11-08 18:24:12 +11:00
if contentType == zipMimeType || contentType == windowsZipMimeType {
2024-01-31 14:49:55 +01:00
zipReader , err := zip . NewReader ( bytes . NewReader ( data ), int64 ( len ( data )))
if err != nil {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Incomplete .zip archive file." ,
Detail : err . Error (),
})
return
}
2024-10-25 15:14:39 +01:00
data , err = archive . CreateTarFromZip ( zipReader , HTTPFileMaxBytes )
2024-01-31 14:49:55 +01:00
if err != nil {
httpapi . Write ( ctx , rw , http . StatusInternalServerError , codersdk . Response {
Message : "Internal error processing .zip archive." ,
Detail : err . Error (),
})
return
}
contentType = tarMimeType
}
2022-02-08 12:00:44 -06:00
hashBytes := sha256 . Sum256 ( data )
2022-02-12 13:34:04 -06:00
hash := hex . EncodeToString ( hashBytes [:])
2022-10-13 18:02:52 -05:00
file , err := api . Database . GetFileByHashAndCreator ( ctx , database . GetFileByHashAndCreatorParams {
Hash : hash ,
CreatedBy : apiKey . UserID ,
})
2022-02-12 13:34:04 -06:00
if err == nil {
// The file already exists!
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusOK , codersdk . UploadResponse {
2022-10-13 18:02:52 -05:00
ID : file . ID ,
2022-02-12 13:34:04 -06:00
})
return
2023-02-14 08:27:06 -06:00
} else if ! errors . Is ( err , sql . ErrNoRows ) {
httpapi . Write ( ctx , rw , http . StatusInternalServerError , codersdk . Response {
Message : "Internal error getting file." ,
Detail : err . Error (),
})
return
2022-02-12 13:34:04 -06:00
}
2023-02-14 08:27:06 -06:00
2022-10-13 18:02:52 -05:00
id := uuid . New ()
2022-09-21 17:07:00 -05:00
file , err = api . Database . InsertFile ( ctx , database . InsertFileParams {
2022-10-13 18:02:52 -05:00
ID : id ,
2022-02-12 13:34:04 -06:00
Hash : hash ,
2022-02-08 12:00:44 -06:00
CreatedBy : apiKey . UserID ,
2023-09-01 19:50:12 +03:00
CreatedAt : dbtime . Now (),
2022-02-08 12:00:44 -06:00
Mimetype : contentType ,
Data : data ,
})
if err != nil {
2025-07-30 13:55:30 -07:00
if database . IsUniqueViolation ( err , database . UniqueFilesHashCreatedByKey ) {
// The file was uploaded by some concurrent process since the last time we checked for it, fetch it again.
file , err = api . Database . GetFileByHashAndCreator ( ctx , database . GetFileByHashAndCreatorParams {
Hash : hash ,
CreatedBy : apiKey . UserID ,
})
api . Logger . Info ( ctx , "postFile handler hit UniqueViolation trying to upload file after already checking for the file existence" , slog . F ( "hash" , hash ), slog . F ( "created_by_id" , apiKey . UserID ))
}
// At this point the first error was either not the UniqueViolation OR there's still an error even after we
// attempt to fetch the file again, so we should return here.
if err != nil {
httpapi . Write ( ctx , rw , http . StatusInternalServerError , codersdk . Response {
Message : "Internal error saving file." ,
Detail : err . Error (),
})
return
}
2022-02-08 12:00:44 -06:00
}
2022-04-12 10:17:33 -05:00
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusCreated , codersdk . UploadResponse {
2022-10-13 18:02:52 -05:00
ID : file . ID ,
2022-02-08 12:00:44 -06:00
})
}
2022-03-07 11:40:54 -06:00
2023-01-03 19:21:10 +01:00
// @Summary Get file by ID
// @ID get-file-by-id
// @Security CoderSessionToken
// @Tags Files
// @Param fileID path string true "File ID" format(uuid)
// @Success 200
// @Router /files/{fileID} [get]
2022-10-13 18:02:52 -05:00
func ( api * API ) fileByID ( rw http . ResponseWriter , r * http . Request ) {
2024-01-31 14:49:55 +01:00
var (
ctx = r . Context ()
format = r . URL . Query (). Get ( "format" )
)
2022-10-13 18:02:52 -05:00
fileID := chi . URLParam ( r , "fileID" )
if fileID == "" {
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
2022-10-13 18:02:52 -05:00
Message : "File id must be provided in url." ,
2022-03-07 11:40:54 -06:00
})
return
}
2022-10-13 18:02:52 -05:00
id , err := uuid . Parse ( fileID )
if err != nil {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "File id must be a valid UUID." ,
})
return
}
file , err := api . Database . GetFileByID ( ctx , id )
2023-04-13 13:06:16 -05:00
if httpapi . Is404Error ( err ) {
2022-06-14 10:14:05 -05:00
httpapi . ResourceNotFound ( rw )
2022-03-07 11:40:54 -06:00
return
}
if err != nil {
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusInternalServerError , codersdk . Response {
2022-06-07 09:33:06 -05:00
Message : "Internal error fetching file." ,
2022-06-03 16:48:09 -05:00
Detail : err . Error (),
2022-03-07 11:40:54 -06:00
})
return
}
2022-05-24 08:25:02 -05:00
2024-01-31 14:49:55 +01:00
switch format {
case codersdk . FormatZip :
if file . Mimetype != codersdk . ContentTypeTar {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Only .tar files can be converted to .zip format" ,
})
return
}
rw . Header (). Set ( "Content-Type" , codersdk . ContentTypeZip )
rw . WriteHeader ( http . StatusOK )
2024-10-25 15:14:39 +01:00
err = archive . WriteZip ( rw , tar . NewReader ( bytes . NewReader ( file . Data )), HTTPFileMaxBytes )
2024-01-31 14:49:55 +01:00
if err != nil {
api . Logger . Error ( ctx , "invalid .zip archive" , slog . F ( "file_id" , fileID ), slog . F ( "mimetype" , file . Mimetype ), slog . Error ( err ))
}
case "" : // no format? no conversion
rw . Header (). Set ( "Content-Type" , file . Mimetype )
_ , _ = rw . Write ( file . Data )
default :
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Unsupported conversion format." ,
})
}
2022-03-07 11:40:54 -06:00
}