|
|
|
package namespace
|
|
|
|
|
|
|
|
type FileSizeQuota struct {
|
|
|
|
AllowanceKB int64
|
|
|
|
CurrentUsage int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileSizeQuota) Allows(size int64) bool {
|
|
|
|
return f.Remaining() >= size
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileSizeQuota) Remaining() int64 {
|
|
|
|
return f.AllowanceKB - f.CurrentUsage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileSizeQuota) Add(size int64) error {
|
|
|
|
if !f.Allows(size) {
|
|
|
|
return ErrExceedQuota
|
|
|
|
}
|
|
|
|
|
|
|
|
f.CurrentUsage += size
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileSizeQuota) Remove(size int64) error {
|
|
|
|
if size > f.CurrentUsage {
|
|
|
|
return ErrQuotaInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
f.CurrentUsage -= size
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Usage struct {
|
|
|
|
Stored int64
|
|
|
|
Uploads int64
|
|
|
|
UploadB int64
|
|
|
|
Downloads int64
|
|
|
|
DownloadB int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u Usage) Uploaded(size int64) Usage {
|
|
|
|
return Usage{
|
|
|
|
u.Stored,
|
|
|
|
u.Uploads + 1,
|
|
|
|
u.UploadB + size,
|
|
|
|
u.Downloads,
|
|
|
|
u.DownloadB,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u Usage) Downloaded(size int64) Usage {
|
|
|
|
return Usage{
|
|
|
|
u.Stored,
|
|
|
|
u.Uploads,
|
|
|
|
u.UploadB,
|
|
|
|
u.Downloads + 1,
|
|
|
|
u.DownloadB + size,
|
|
|
|
}
|
|
|
|
}
|