mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore(coderd): extract fileszip to package archive for reuse (#15229)
Related to https://github.com/coder/coder/issues/15087 As part of sniffing the workspace tags from an uploaded file, we need to be able to handle both zip and tar files. Extracting the functions to a separate `archive` package will be helpful here.
This commit is contained in:
+5
-4
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/v2/archive"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
@@ -27,7 +28,7 @@ const (
|
||||
tarMimeType = "application/x-tar"
|
||||
zipMimeType = "application/zip"
|
||||
|
||||
httpFileMaxBytes = 10 * (10 << 20)
|
||||
HTTPFileMaxBytes = 10 * (10 << 20)
|
||||
)
|
||||
|
||||
// @Summary Upload file
|
||||
@@ -55,7 +56,7 @@ func (api *API) postFile(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(rw, r.Body, httpFileMaxBytes)
|
||||
r.Body = http.MaxBytesReader(rw, r.Body, HTTPFileMaxBytes)
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
|
||||
@@ -75,7 +76,7 @@ func (api *API) postFile(rw http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
data, err = CreateTarFromZip(zipReader)
|
||||
data, err = archive.CreateTarFromZip(zipReader, HTTPFileMaxBytes)
|
||||
if err != nil {
|
||||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
||||
Message: "Internal error processing .zip archive.",
|
||||
@@ -181,7 +182,7 @@ func (api *API) fileByID(rw http.ResponseWriter, r *http.Request) {
|
||||
|
||||
rw.Header().Set("Content-Type", codersdk.ContentTypeZip)
|
||||
rw.WriteHeader(http.StatusOK)
|
||||
err = WriteZipArchive(rw, tar.NewReader(bytes.NewReader(file.Data)))
|
||||
err = archive.WriteZip(rw, tar.NewReader(bytes.NewReader(file.Data)), HTTPFileMaxBytes)
|
||||
if err != nil {
|
||||
api.Logger.Error(ctx, "invalid .zip archive", slog.F("file_id", fileID), slog.F("mimetype", file.Mimetype), slog.Error(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user