示例#1
0
func (s *Disk) downloadIndex(ctx context.Context, index types.ObjectIndex) (*os.File, *http.Response, error) {
	vhost, ok := contexts.GetVhost(ctx)
	if !ok {
		return nil, nil, fmt.Errorf("Could not get vhost from context.")
	}

	startOffset := uint64(index.Part) * s.partSize
	endOffset := startOffset + s.partSize - 1
	resp, err := vhost.Upstream.GetRequestPartial(index.ObjID.Path, startOffset, endOffset)
	if err != nil {
		return nil, nil, err
	}
	defer resp.Body.Close()
	file, err := CreateFile(path.Join(s.path, pathFromIndex(index)))
	if err != nil {
		return nil, nil, err
	}

	size, err := io.Copy(file, resp.Body)
	if err != nil {
		return nil, nil, utils.NewCompositeError(err, file.Close())
	}
	s.logger.Debugf("Storage [%p] downloaded for index %s with size %d", s, index, size)

	_, err = file.Seek(0, os.SEEK_SET)
	if err != nil {
		return nil, nil, utils.NewCompositeError(err, file.Close())
	}

	return file, resp, err
}
示例#2
0
func downloadHeaders(ctx context.Context, hq *headerQueue, finished chan<- *headerQueue) {
	vhost, ok := contexts.GetVhost(ctx)
	if !ok {
		hq.err = fmt.Errorf("Could not get vhost from context.")
		finished <- hq
		return
	}

	resp, err := vhost.Upstream.GetHeader(hq.id.Path)
	if err != nil {
		hq.err = err
	} else {
		hq.header = resp.Header
		//!TODO: handle allowed cache duration
		hq.isCacheable, _ = utils.IsResponseCacheable(resp)
	}

	finished <- hq
}
示例#3
0
// GetFullFile returns the whole file specified by the ObjectID
func (s *Disk) GetFullFile(ctx context.Context, id types.ObjectID) (io.ReadCloser, error) {
	vhost, ok := contexts.GetVhost(ctx)
	if !ok {
		return nil, fmt.Errorf("Could not get vhost from context.")
	}

	size, err := vhost.Upstream.GetSize(id.Path)
	if err != nil {
		return nil, err
	}
	if size <= 0 {
		resp, err := vhost.Upstream.GetRequest(id.Path)
		if err != nil {
			return nil, err
		}

		return resp.Body, nil
	}

	return s.Get(ctx, id, 0, uint64(size))
}