Use stat(2) for calculating file's offset

This commit is contained in:
Marius 2016-02-21 23:38:38 +01:00
parent 13c27e1c19
commit eb9100e9bb
1 changed files with 13 additions and 31 deletions

View File

@ -70,11 +70,6 @@ func (store FileStore) WriteChunk(id string, offset int64, src io.Reader) (int64
defer file.Close() defer file.Close()
n, err := io.Copy(file, src) n, err := io.Copy(file, src)
if n > 0 {
if err := store.setOffset(id, offset+n); err != nil {
return 0, err
}
}
return n, err return n, err
} }
@ -84,10 +79,20 @@ func (store FileStore) GetInfo(id string) (tusd.FileInfo, error) {
if err != nil { if err != nil {
return info, err return info, err
} }
err = json.Unmarshal(data, &info) if err := json.Unmarshal(data, &info); err != nil {
return info, err return info, err
} }
stat, err := os.Stat(store.binPath(id))
if err != nil {
return info, err
}
info.Offset = stat.Size()
return info, nil
}
func (store FileStore) GetReader(id string) (io.Reader, error) { func (store FileStore) GetReader(id string) (io.Reader, error) {
return os.Open(store.binPath(id)) return os.Open(store.binPath(id))
} }
@ -109,20 +114,13 @@ func (store FileStore) ConcatUploads(dest string, uploads []string) (err error)
} }
defer file.Close() defer file.Close()
var bytesRead int64
defer func() {
err = store.setOffset(dest, bytesRead)
}()
for _, id := range uploads { for _, id := range uploads {
src, err := store.GetReader(id) src, err := store.GetReader(id)
if err != nil { if err != nil {
return err return err
} }
n, err := io.Copy(file, src) if _, err := io.Copy(file, src); err != nil {
bytesRead += n
if err != nil {
return err return err
} }
} }
@ -193,19 +191,3 @@ func (store FileStore) writeInfo(id string, info tusd.FileInfo) error {
} }
return ioutil.WriteFile(store.infoPath(id), data, defaultFilePerm) return ioutil.WriteFile(store.infoPath(id), data, defaultFilePerm)
} }
// setOffset updates the .info file to match the new offset.
func (store FileStore) setOffset(id string, offset int64) error {
info, err := store.GetInfo(id)
if err != nil {
return err
}
// never decrement the offset
if info.Offset >= offset {
return nil
}
info.Offset = offset
return store.writeInfo(id, info)
}