You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
3.3 KiB
Go
162 lines
3.3 KiB
Go
3 years ago
|
package dataswamp
|
||
|
|
||
|
import (
|
||
3 years ago
|
"caj-larsson/bog/dataswamp/namespace"
|
||
|
"caj-larsson/bog/dataswamp/swampfile"
|
||
3 years ago
|
"io"
|
||
3 years ago
|
"strconv"
|
||
|
"time"
|
||
3 years ago
|
// "errors"
|
||
|
// "fmt"
|
||
3 years ago
|
)
|
||
|
|
||
|
type SwampFileService struct {
|
||
3 years ago
|
namespace_repo namespace.Repository
|
||
|
swamp_file_repo swampfile.Repository
|
||
|
default_allowance_bytes int64
|
||
3 years ago
|
default_allowance_duration time.Duration
|
||
3 years ago
|
logger Logger
|
||
3 years ago
|
}
|
||
|
|
||
|
func NewSwampFileService(
|
||
|
namespace_repo namespace.Repository,
|
||
|
swamp_file_repo swampfile.Repository,
|
||
|
da_bytes int64,
|
||
|
da_duration time.Duration,
|
||
3 years ago
|
logger Logger,
|
||
3 years ago
|
) SwampFileService {
|
||
3 years ago
|
return SwampFileService{namespace_repo, swamp_file_repo, da_bytes, da_duration, logger}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
func (s SwampFileService) getOrCreateNs(namespace_in string) *namespace.Namespace {
|
||
3 years ago
|
ns, err := s.namespace_repo.GetByName(namespace_in)
|
||
|
|
||
|
if err == namespace.ErrNotExists {
|
||
3 years ago
|
new_ns := namespace.Namespace{
|
||
3 years ago
|
0,
|
||
|
namespace_in,
|
||
|
time.Now(),
|
||
|
s.default_allowance_duration,
|
||
3 years ago
|
namespace.FileSizeQuota{s.default_allowance_bytes, 0},
|
||
3 years ago
|
namespace.FileStat{0, 0},
|
||
|
namespace.FileStat{0, 0},
|
||
|
namespace.FileStat{0, 0},
|
||
3 years ago
|
}
|
||
|
created_ns, err := s.namespace_repo.Create(new_ns)
|
||
|
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
return created_ns
|
||
|
}
|
||
|
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
return ns
|
||
|
}
|
||
|
|
||
|
func (s SwampFileService) SaveFile(ref swampfile.FileReference, src io.Reader, size int64) error {
|
||
|
ns := s.getOrCreateNs(ref.UserAgent)
|
||
|
|
||
3 years ago
|
r, err := ref.Clean(true)
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
if !ns.FileQuota.Allows(size) {
|
||
|
return namespace.ErrExceedQuota
|
||
|
}
|
||
|
|
||
3 years ago
|
f, err := s.swamp_file_repo.Create(r.Path, strconv.FormatInt(ns.ID, 10))
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
written, err := io.CopyN(f, src, size)
|
||
|
|
||
|
if written < size {
|
||
3 years ago
|
s.swamp_file_repo.Delete(r.Path, strconv.FormatInt(ns.ID, 10))
|
||
3 years ago
|
return swampfile.ErrContentSizeExaggerated
|
||
|
}
|
||
|
|
||
|
var buf = make([]byte, 1)
|
||
|
|
||
3 years ago
|
overread, err := src.Read(buf)
|
||
3 years ago
|
|
||
|
if overread > 0 || err != io.EOF {
|
||
3 years ago
|
s.swamp_file_repo.Delete(r.Path, strconv.FormatInt(ns.ID, 10))
|
||
3 years ago
|
return swampfile.ErrContentSizeExceeded
|
||
|
}
|
||
|
|
||
3 years ago
|
f.Close()
|
||
3 years ago
|
uq, err := ns.FileQuota.Add(size)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
ns.FileQuota = *uq
|
||
3 years ago
|
ns.Usage = ns.Usage.Add(size)
|
||
|
ns.Upload = ns.Upload.Add(size)
|
||
3 years ago
|
s.namespace_repo.Update(ns.ID, *ns)
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (s SwampFileService) OpenOutFile(ref swampfile.FileReference) (swampfile.SwampOutFile, error) {
|
||
3 years ago
|
ns := s.getOrCreateNs(ref.UserAgent)
|
||
3 years ago
|
|
||
3 years ago
|
r, err := ref.Clean(true)
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
f, err := s.swamp_file_repo.Open(r.Path, strconv.FormatInt(ns.ID, 10))
|
||
3 years ago
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
3 years ago
|
ns.Download = ns.Download.Add(f.Size())
|
||
|
s.namespace_repo.Update(ns.ID, *ns)
|
||
|
|
||
3 years ago
|
return f, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
func (s SwampFileService) NamespaceStats() ([]namespace.Namespace, error) {
|
||
|
return s.namespace_repo.All()
|
||
|
}
|
||
|
|
||
3 years ago
|
func (s SwampFileService) CleanUpExpiredFiles() error {
|
||
3 years ago
|
s.logger.Info("Cleaning up expired files")
|
||
3 years ago
|
nss, err := s.namespace_repo.All()
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for _, ns := range nss {
|
||
|
expiry := time.Now().Add(-ns.AllowanceDuration)
|
||
|
dfs, err := s.swamp_file_repo.DeleteOlderThan(strconv.FormatInt(ns.ID, 10), expiry)
|
||
|
|
||
|
for _, df := range dfs {
|
||
3 years ago
|
dq, err := ns.FileQuota.Remove(df.Size)
|
||
|
if err != nil {
|
||
|
dq.CurrentUsage = 0
|
||
|
}
|
||
|
ns.FileQuota = *dq
|
||
3 years ago
|
}
|
||
|
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
s.namespace_repo.Update(ns.ID, ns)
|
||
|
}
|
||
|
return nil
|
||
3 years ago
|
}
|