// InitAssetsTemplates initializes the router to use either a ricebox or the // filesystem in case the ricebox couldn't be found. func InitAssetsTemplates(r *gin.Engine, tbox, abox *rice.Box, verbose bool, names ...string) error { var err error if tbox != nil { mt := multitemplate.New() var tmpl string var message *template.Template for _, x := range names { if tmpl, err = tbox.String(x); err != nil { return err } if message, err = template.New(x).Parse(tmpl); err != nil { return err } mt.Add(x, message) } logger.Debug("server", "Loaded templates from \"templates\" box") r.HTMLRender = mt } else { r.LoadHTMLGlob("templates/*") logger.Debug("server", "Loaded templates from disk") } if abox != nil { r.StaticFS("/static", abox.HTTPBox()) logger.Debug("server", "Loaded assets from \"assets\" box") } else { r.Static("/static", "assets") logger.Debug("server", "Loaded assets from disk") } return nil }
// Initialize loads the previous state of the statistics func Initialize() error { logger.Debug("server", "Started Initialize on statistics object") var cfiles uint64 var csize uint64 var err error err = database.DB.View(func(tx *bolt.Tx) error { S.Decode(tx.Bucket([]byte("statistics")).Get([]byte("main"))) return tx.Bucket([]byte("resources")).ForEach(func(k, v []byte) error { r := &Resource{} if err = r.Decode(v); err != nil { return err } csize += uint64(r.Size) cfiles++ return nil }) }) if err != nil { logger.Err("server", "Could not initialize statistics") return err } S.CurrentFiles = cfiles S.CurrentSize = csize logger.Info("server", fmt.Sprintf("Total %d (%s)", S.TotalFiles, utils.HumanBytes(S.TotalSize))) logger.Info("server", fmt.Sprintf("Current %d (%s)", cfiles, utils.HumanBytes(csize))) logger.Debug("server", "Done Initialize on statistics object") return err }
// Get retrives the Resource from the bucket func (r *Resource) Get(key string) error { logger.Debug("server", "Started Get on Resource", key) err := database.DB.View(func(tx *bolt.Tx) error { return r.Decode(tx.Bucket([]byte("resources")).Get([]byte(key))) }) logger.Debug("server", "Done Get on Resource", key) return err }
// Monit monitors the database and file system to remove old entries func Monit() { logger.Info("monitoring", "Started Monitoring") var err error tc := time.NewTicker(1 * time.Minute) for { logger.Debug("monitoring", "Started Monit on Resources") now := time.Now() found := 0 var sizeRemoved uint64 err = database.DB.Update(func(tx *bolt.Tx) error { b := tx.Bucket([]byte("resources")) err = b.ForEach(func(k, v []byte) error { var err error r := &models.Resource{} if err = r.Decode(v); err != nil { return err } if r.DeleteAt.Before(now) { if err = b.Delete([]byte(r.Key)); err != nil { logger.Err("monitoring", "While deleting entry (skipped)", err) } else { if err = os.Remove(path.Join(conf.C.UploadDir, r.Key)); err != nil { logger.Err("monitoring", "While deleting file (skipped)", err) } else { found++ sizeRemoved += uint64(r.Size) } } } return nil }) if err != nil { return err } if found > 0 { var data []byte models.S.CurrentFiles -= uint64(found) models.S.CurrentSize -= sizeRemoved if data, err = models.S.Encode(); err != nil { return err } return tx.Bucket([]byte("statistics")).Put([]byte("main"), data) } return nil }) if err != nil { logger.Err("monitoring", "While monitoring", err) } else { if found > 0 { logger.Info("monitoring", fmt.Sprintf("Deleted %d entries and files in %s", found, time.Since(now))) logger.Info("monitoring", fmt.Sprintf("Serving %d (%s) files", models.S.CurrentFiles, utils.HumanBytes(models.S.CurrentSize))) } logger.Debug("monitoring", fmt.Sprintf("Serving %d (%s) files", models.S.CurrentFiles, utils.HumanBytes(models.S.CurrentSize))) } logger.Debug("monitoring", fmt.Sprintf("Done Monit on Resources (%s)", time.Since(now))) <-tc.C } }
// Save writes the Resource to the bucket func (s Statistics) Save() error { logger.Debug("server", "Started Save on statistics object") var err error var data []byte if data, err = s.Encode(); err != nil { return err } err = database.DB.Update(func(tx *bolt.Tx) error { return tx.Bucket([]byte("statistics")).Put([]byte("main"), data) }) logger.Debug("server", "Done Save on statistics object") return err }
// Delete deletes a resource in database and on disk func (r Resource) Delete() error { logger.Debug("server", "Started Delete on Resource", r.Key) var err error err = database.DB.Update(func(tx *bolt.Tx) error { if err = tx.Bucket([]byte("resources")).Delete([]byte(r.Key)); err != nil { return err } S.CurrentFiles-- S.CurrentSize -= uint64(r.Size) var data []byte if data, err = S.Encode(); err != nil { return err } return tx.Bucket([]byte("statistics")).Put([]byte("main"), data) }) if err != nil { return err } err = os.Remove(path.Join(conf.C.UploadDir, r.Key)) logger.Debug("server", "Done Delete on Resource", r.Key) logger.Debug("server", fmt.Sprintf("Serving %d (%s) files", S.CurrentFiles, utils.HumanBytes(S.CurrentSize))) return err }
// Save writes the Resource to the bucket func (r Resource) Save() error { logger.Debug("server", "Started Save on Resource", r.Key) var err error var data []byte if data, err = r.Encode(); err != nil { return err } err = database.DB.Update(func(tx *bolt.Tx) error { if err = tx.Bucket([]byte("resources")).Put([]byte(r.Key), data); err != nil { return err } S.TotalFiles++ S.TotalSize += uint64(r.Size) S.CurrentFiles++ S.CurrentSize += uint64(r.Size) if data, err = S.Encode(); err != nil { return err } return tx.Bucket([]byte("statistics")).Put([]byte("main"), data) }) logger.Debug("server", "Done Save on Resource", r.Key) return err }