diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2023-09-25 22:00:46 +0200 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2023-09-25 22:00:46 +0200 |
commit | 08287375736a0a57c03b4bd6d9c222dc5567db6e (patch) | |
tree | b6426ca7127cc65484034542790b75773e5282f5 /website.go | |
parent | bc368943a4c0853718b8a53b1caadc297412ef32 (diff) | |
download | guichet-08287375736a0a57c03b4bd6d9c222dc5567db6e.tar.gz guichet-08287375736a0a57c03b4bd6d9c222dc5567db6e.zip |
patch and delete with quota are now implemented
Diffstat (limited to 'website.go')
-rw-r--r-- | website.go | 111 |
1 files changed, 62 insertions, 49 deletions
@@ -15,55 +15,11 @@ var ( ErrCantCreateBucket = fmt.Errorf("Can't create this bucket. Maybe another bucket already exists with this name or you have an invalid character") ErrCantAllowKey = fmt.Errorf("Can't allow given key on the target bucket") ErrCantConfigureBucket = fmt.Errorf("Unable to configure the bucket (activating website, adding quotas, etc.)") + ErrBucketDeleteNotEmpty = fmt.Errorf("You must remove all the files before deleting a bucket") + ErrBucketDeleteUnfinishedUpload = fmt.Errorf("You must remove all the unfinished multipart uploads before deleting a bucket") ) -type QuotaStat struct { - Current int64 - Max int64 - Ratio float64 - Burstable bool -} -func NewQuotaStat(current, max int64, burstable bool) QuotaStat { - return QuotaStat { - Current: current, - Max: max, - Ratio: float64(current) / float64(max), - Burstable: burstable, - } -} -func (q *QuotaStat) IsFull() bool { - return q.Current >= q.Max -} -func (q *QuotaStat) Percent() int64 { - return int64(q.Ratio * 100) -} -func (q *QuotaStat) PrettyCurrent() string { - return prettyValue(q.Current) -} -func (q *QuotaStat) PrettyMax() string { - return prettyValue(q.Max) -} - -func prettyValue(v int64) string { - if v < 1024 { - return fmt.Sprintf("%d octets", v) - } - v = v / 1024 - if v < 1024 { - return fmt.Sprintf("%d kio", v) - } - v = v / 1024 - if v < 1024 { - return fmt.Sprintf("%d Mio", v) - } - v = v / 1024 - if v < 1024 { - return fmt.Sprintf("%d Gio", v) - } - v = v / 1024 - return fmt.Sprintf("%d Tio", v) -} type WebsiteId struct { Pretty string @@ -146,8 +102,36 @@ func (w *WebsiteController) Inspect(pretty string) (*WebsiteView, error) { return NewWebsiteView(binfo), nil } -func (w *WebsiteController) Patch(patch *WebsitePatch) (*WebsiteView, error) { - return nil, nil +func (w *WebsiteController) Patch(pretty string, patch *WebsitePatch) (*WebsiteView, error) { + website, ok := w.WebsiteIdx[pretty] + if !ok { + return nil, ErrWebsiteNotFound + } + + binfo, err := grgGetBucket(website.Internal) + if err != nil { + return nil, ErrFetchBucketInfo + } + + // Patch the max size + urQuota := garage.NewUpdateBucketRequestQuotas() + urQuota.SetMaxSize(w.User.Quota.WebsiteSizeAdjust(binfo.Quotas.GetMaxSize())) + urQuota.SetMaxObjects(w.User.Quota.WebsiteObjectAdjust(binfo.Quotas.GetMaxObjects())) + if patch.size != nil { + urQuota.SetMaxSize(w.User.Quota.WebsiteSizeAdjust(*patch.size)) + } + + // Build the update + ur := garage.NewUpdateBucketRequest() + ur.SetQuotas(*urQuota) + + // Call garage + binfo, err = grgUpdateBucket(website.Internal, ur) + if err != nil { + return nil, ErrCantConfigureBucket + } + + return NewWebsiteView(binfo), nil } func (w *WebsiteController) Create(pretty string) (*WebsiteView, error) { @@ -190,6 +174,35 @@ func (w *WebsiteController) Create(pretty string) (*WebsiteView, error) { return NewWebsiteView(binfo), nil } +func (w *WebsiteController) Delete(pretty string) error { + if pretty == "" { + return ErrEmptyBucketName + } + + website, ok := w.WebsiteIdx[pretty] + if !ok { + return ErrWebsiteNotFound + } + + binfo, err := grgGetBucket(website.Internal) + if err != nil { + return ErrFetchBucketInfo + } + + if *binfo.Objects > int64(0) { + return ErrBucketDeleteNotEmpty + } + + if *binfo.UnfinishedUploads > int32(0) { + return ErrBucketDeleteUnfinishedUpload + } + + err = grgDeleteBucket(website.Internal) + return err +} + + + type WebsiteView struct { Name *WebsiteId @@ -207,5 +220,5 @@ func NewWebsiteView(binfo *garage.BucketInfo) *WebsiteView { } type WebsitePatch struct { - size int64 + size *int64 } |