71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
package photosstore
|
|
|
|
import (
|
|
"errors"
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
var (
|
|
ErrStoreMissingChunks = errors.New("part checksum missing")
|
|
)
|
|
|
|
type StoreReaderChunk struct {
|
|
Filename string
|
|
Size int64
|
|
}
|
|
|
|
type StoreReader struct {
|
|
current *os.File
|
|
chunk int
|
|
chunks []StoreReaderChunk
|
|
Size int64
|
|
}
|
|
|
|
func (s *Store) NewStoreReader(chunks []string) (*StoreReader, error) {
|
|
sr := &StoreReader{nil, 0, make([]StoreReaderChunk, len(chunks)), 0}
|
|
for i, chunk := range chunks {
|
|
c := s.Chunk(chunk)
|
|
name := c.Filename()
|
|
size := c.Size()
|
|
if size < 0 {
|
|
return nil, ErrStoreMissingChunks
|
|
}
|
|
sr.chunks[i] = StoreReaderChunk{name, size}
|
|
sr.Size += size
|
|
}
|
|
return sr, nil
|
|
}
|
|
|
|
func (s *StoreReader) Read(p []byte) (n int, err error) {
|
|
if s.current == nil {
|
|
f, err := os.Open(s.chunks[s.chunk].Filename)
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
s.current = f
|
|
}
|
|
|
|
n, err = s.current.Read(p)
|
|
if err == io.EOF {
|
|
s.chunk++
|
|
if s.chunk > len(s.chunks)-1 {
|
|
return
|
|
}
|
|
s.Close()
|
|
return s.Read(p)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (s *StoreReader) Close() {
|
|
if s.current != nil {
|
|
s.current.Close()
|
|
s.current = nil
|
|
}
|
|
}
|
|
|
|
// func (s *StoreReader) Seek(offset int64, whence int) (int64, error) {
|
|
|
|
// }
|