Finish DataStore

This commit is contained in:
Felix Geisendörfer 2013-03-19 10:36:08 +01:00
parent 02b0a3b697
commit 99b15cf7af
3 changed files with 17 additions and 49 deletions

View File

@ -1,46 +0,0 @@
package main
// This is very simple for now and will be enhanced as needed.
import (
"io"
"os"
"path"
)
var dataDir string
func init() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
dataDir = path.Join(wd, "tus_data")
if err := os.MkdirAll(dataDir, 0777); err != nil {
panic(err)
}
}
func dataPath(fileId string) string {
return path.Join(dataDir, fileId)
}
func logPath(fileId string) string {
return dataPath(fileId) + ".log"
}
func getFileData(fileId string) (io.ReadCloser, int64, error) {
d := dataPath(fileId)
file, err := os.Open(d)
if err != nil {
return nil, 0, err
}
stat, err := file.Stat()
if err != nil {
return nil, 0, err
}
return file, stat.Size(), nil
}

View File

@ -34,7 +34,7 @@ func (s *DataStore) CreateFile(id string, size int64) error {
}
func (s *DataStore) WriteFileChunk(id string, start int64, end int64, src io.Reader) error {
file, err := os.OpenFile(dataPath(id), os.O_WRONLY, 0666)
file, err := os.OpenFile(s.filePath(id), os.O_WRONLY, 0666)
if err != nil {
return err
}
@ -92,6 +92,20 @@ func (s *DataStore) GetFileChunks(id string) (chunkSet, error) {
return chunks, nil
}
func (s *DataStore) ReadFile(id string) (io.ReadCloser, int64, error) {
file, err := os.Open(s.filePath(id))
if err != nil {
return nil, 0, err
}
stat, err := file.Stat()
if err != nil {
return nil, 0, err
}
return file, stat.Size(), nil
}
func (s *DataStore) appendFileLog(id string, entry string) error {
logPath := s.logPath(id)
logFile, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0666)

View File

@ -20,7 +20,7 @@ func init() {
panic(err)
}
dataDir = path.Join(wd, "tus_data")
dataDir := path.Join(wd, "tus_data")
if err := os.MkdirAll(dataDir, 0777); err != nil {
panic(err)
}
@ -106,7 +106,7 @@ func headFile(w http.ResponseWriter, r *http.Request, fileId string) {
}
func getFile(w http.ResponseWriter, r *http.Request, fileId string) {
data, size, err := getFileData(fileId)
data, size, err := dataStore.ReadFile(fileId)
if err != nil {
// @TODO: Could be a 404 as well
reply(w, http.StatusInternalServerError, err.Error())