use a new thread safe map
This commit is contained in:
@@ -112,7 +112,6 @@ func (d *Downloader) downloadFromAPI(request *Request, buffer []byte, delay int6
|
||||
downloadURL := resp.Download
|
||||
req, err := http.NewRequest("GET", downloadURL, nil)
|
||||
if nil != err {
|
||||
d.log.Debugf("request init error: %v", err)
|
||||
return fmt.Errorf("could not create request object %s %s from API", request.file.Path, request.file.Link)
|
||||
}
|
||||
req.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", request.offsetStart, request.offsetEnd-1))
|
||||
|
||||
@@ -381,23 +381,18 @@ func (s *Storage) Store(id RequestID, bytes []byte) (err error) {
|
||||
|
||||
if nil != chunk {
|
||||
if chunk.valid(id) {
|
||||
s.log.Debugf("Create chunk %v (exists: valid)", id)
|
||||
return nil
|
||||
}
|
||||
s.log.Warnf("Create chunk %v(exists: overwrite)", id)
|
||||
} else {
|
||||
index := s.stack.Pop()
|
||||
if index == -1 {
|
||||
s.log.Debugf("Create chunk %v (failed)", id)
|
||||
return fmt.Errorf("no buffers available")
|
||||
}
|
||||
chunk = s.buffers[index]
|
||||
deleteID := chunk.id
|
||||
if blankRequestID != deleteID {
|
||||
delete(s.chunks, deleteID)
|
||||
s.log.Debugf("Create chunk %v (reused)", id)
|
||||
} else {
|
||||
s.log.Debugf("Create chunk %v (stored)", id)
|
||||
}
|
||||
s.chunks[id] = index
|
||||
chunk.item = s.stack.Push(index)
|
||||
|
||||
@@ -29,6 +29,23 @@ type Torrent struct {
|
||||
Links []string `json:"links"`
|
||||
}
|
||||
|
||||
func (i *Torrent) UnmarshalJSON(data []byte) error {
|
||||
type Alias Torrent
|
||||
aux := &struct {
|
||||
Progress float64 `json:"progress"`
|
||||
*Alias
|
||||
}{
|
||||
Alias: (*Alias)(i),
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, &aux); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
i.Progress = int(math.Round(aux.Progress))
|
||||
return nil
|
||||
}
|
||||
|
||||
type TorrentInfo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"filename"`
|
||||
|
||||
Reference in New Issue
Block a user