Exemple #1
0
// ReadResource extracts the relevant info from the request.
func (uh UploadHandler) ReadResource(req *http.Request) (*UploadedResource, error) {
	uReq, err := api.ExtractUploadRequest(req)
	if err != nil {
		return nil, errors.Trace(err)
	}
	var res resource.Resource
	if uReq.PendingID != "" {
		res, err = uh.Store.GetPendingResource(uReq.Service, uReq.Name, uReq.PendingID)
		if err != nil {
			return nil, errors.Trace(err)
		}
	} else {
		res, err = uh.Store.GetResource(uReq.Service, uReq.Name)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}

	chRes, err := uh.updateResource(res.Resource, uReq.Fingerprint, uReq.Size)
	if err != nil {
		return nil, errors.Trace(err)
	}

	uploaded := &UploadedResource{
		Service:   uReq.Service,
		PendingID: uReq.PendingID,
		Resource:  chRes,
		Data:      req.Body,
	}
	return uploaded, nil
}
Exemple #2
0
// ReadResource extracts the relevant info from the request.
func (uh UploadHandler) ReadResource(req *http.Request) (*UploadedResource, error) {
	uReq, err := api.ExtractUploadRequest(req)
	if err != nil {
		return nil, errors.Trace(err)
	}
	var res resource.Resource
	if uReq.PendingID != "" {
		res, err = uh.Store.GetPendingResource(uReq.Service, uReq.Name, uReq.PendingID)
		if err != nil {
			return nil, errors.Trace(err)
		}
	} else {
		res, err = uh.Store.GetResource(uReq.Service, uReq.Name)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}

	ext := path.Ext(res.Path)
	if path.Ext(uReq.Filename) != ext {
		return nil, errors.Errorf("incorrect extension on resource upload %q, expected %q", uReq.Filename, ext)
	}

	chRes, err := uh.updateResource(res.Resource, uReq.Fingerprint, uReq.Size)
	if err != nil {
		return nil, errors.Trace(err)
	}

	uploaded := &UploadedResource{
		Service:   uReq.Service,
		PendingID: uReq.PendingID,
		Resource:  chRes,
		Data:      req.Body,
	}
	return uploaded, nil
}