2022-02-08 18:00:44 +00:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2022-03-07 17:40:54 +00:00
|
|
|
"database/sql"
|
2022-02-08 18:00:44 +00:00
|
|
|
"encoding/hex"
|
2022-03-07 17:40:54 +00:00
|
|
|
"errors"
|
2022-02-08 18:00:44 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-02-08 18:00:44 +00:00
|
|
|
"github.com/go-chi/render"
|
|
|
|
|
2022-03-25 21:07:45 +00:00
|
|
|
"github.com/coder/coder/coderd/database"
|
|
|
|
"github.com/coder/coder/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2022-03-22 19:17:50 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
2022-02-08 18:00:44 +00:00
|
|
|
)
|
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
func (api *api) postFile(rw http.ResponseWriter, r *http.Request) {
|
2022-02-08 18:00:44 +00:00
|
|
|
apiKey := httpmw.APIKey(r)
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
switch contentType {
|
|
|
|
case "application/x-tar":
|
|
|
|
default:
|
|
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("unsupported content type: %s", contentType),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Body = http.MaxBytesReader(rw, r.Body, 10*(10<<20))
|
|
|
|
data, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("read file: %s", err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hashBytes := sha256.Sum256(data)
|
2022-02-12 19:34:04 +00:00
|
|
|
hash := hex.EncodeToString(hashBytes[:])
|
|
|
|
file, err := api.Database.GetFileByHash(r.Context(), hash)
|
|
|
|
if err == nil {
|
|
|
|
// The file already exists!
|
|
|
|
render.Status(r, http.StatusOK)
|
2022-03-22 19:17:50 +00:00
|
|
|
render.JSON(rw, r, codersdk.UploadResponse{
|
2022-02-12 19:34:04 +00:00
|
|
|
Hash: file.Hash,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file, err = api.Database.InsertFile(r.Context(), database.InsertFileParams{
|
|
|
|
Hash: hash,
|
2022-02-08 18:00:44 +00:00
|
|
|
CreatedBy: apiKey.UserID,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
Mimetype: contentType,
|
|
|
|
Data: data,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("insert file: %s", err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
render.Status(r, http.StatusCreated)
|
2022-03-22 19:17:50 +00:00
|
|
|
render.JSON(rw, r, codersdk.UploadResponse{
|
2022-02-08 18:00:44 +00:00
|
|
|
Hash: file.Hash,
|
|
|
|
})
|
|
|
|
}
|
2022-03-07 17:40:54 +00:00
|
|
|
|
|
|
|
func (api *api) fileByHash(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
hash := chi.URLParam(r, "hash")
|
|
|
|
if hash == "" {
|
|
|
|
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{
|
|
|
|
Message: "hash must be provided",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
file, err := api.Database.GetFileByHash(r.Context(), hash)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{
|
|
|
|
Message: "no file exists with that hash",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
|
|
|
|
Message: fmt.Sprintf("get file: %s", err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rw.Header().Set("Content-Type", file.Mimetype)
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = rw.Write(file.Data)
|
|
|
|
}
|