2022-09-21 14:30:36 +00:00
|
|
|
package uploadhandler
|
2021-12-02 15:30:40 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
2022-07-21 05:33:08 +00:00
|
|
|
sglog "github.com/sourcegraph/log"
|
2022-12-20 14:24:44 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2022-07-21 05:33:08 +00:00
|
|
|
|
2021-12-02 15:30:40 +00:00
|
|
|
"github.com/sourcegraph/sourcegraph/internal/observation"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// handleEnqueueSinglePayload handles a non-multipart upload. This creates an upload record
|
|
|
|
|
// with state 'queued', proxies the data to the bundle manager, and returns the generated ID.
|
2022-09-21 14:30:36 +00:00
|
|
|
func (h *UploadHandler[T]) handleEnqueueSinglePayload(ctx context.Context, uploadState uploadState[T], body io.Reader) (_ any, statusCode int, err error) {
|
2024-07-09 11:49:55 +00:00
|
|
|
ctx, _, endObservation := h.operations.handleEnqueueSinglePayload.With(ctx, &err, observation.Args{})
|
2021-12-02 15:30:40 +00:00
|
|
|
defer func() {
|
2023-05-16 15:19:27 +00:00
|
|
|
endObservation(1, observation.Args{Attrs: []attribute.KeyValue{
|
|
|
|
|
attribute.Int("statusCode", statusCode),
|
2021-12-02 15:30:40 +00:00
|
|
|
}})
|
|
|
|
|
}()
|
|
|
|
|
|
2024-07-09 11:49:55 +00:00
|
|
|
uploadResult, err := h.enqueuer.EnqueueSinglePayload(ctx, uploadState.metadata, uploadState.uncompressedSize, body)
|
2021-12-02 15:30:40 +00:00
|
|
|
|
2024-07-09 11:49:55 +00:00
|
|
|
if err != nil {
|
2021-12-02 15:30:40 +00:00
|
|
|
return nil, http.StatusInternalServerError, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 05:33:08 +00:00
|
|
|
h.logger.Info(
|
2022-09-21 14:30:36 +00:00
|
|
|
"uploadhandler: enqueued upload",
|
2024-07-09 11:49:55 +00:00
|
|
|
sglog.Int("id", uploadResult.UploadID),
|
2021-12-02 15:30:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// older versions of src-cli expect a string
|
|
|
|
|
return struct {
|
|
|
|
|
ID string `json:"id"`
|
2024-07-09 11:49:55 +00:00
|
|
|
}{ID: strconv.Itoa(uploadResult.UploadID)}, 0, nil
|
2021-12-02 15:30:40 +00:00
|
|
|
}
|