Zip bundles: generate cutesy zip display names
This commit is contained in:
@@ -2,9 +2,11 @@ package file
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
|
"crypto/rand"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math/big"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -36,6 +38,36 @@ func dedupeName(name string, seen map[string]int) string {
|
|||||||
return fmt.Sprintf("%s (%d)%s", base, seen[name], ext)
|
return fmt.Sprintf("%s (%d)%s", base, seen[name], ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func randIndex(n int) int {
|
||||||
|
if n <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
x, err := rand.Int(rand.Reader, big.NewInt(int64(n)))
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return int(x.Int64())
|
||||||
|
}
|
||||||
|
|
||||||
|
func cuteZipName(fileCount int) string {
|
||||||
|
adjectives := []string{
|
||||||
|
"Cool", "Super", "Hot", "Spicy", "Sneaky", "Sleepy", "Tiny", "Mega", "Cosmic", "Silly",
|
||||||
|
"Cursed", "Blessed", "Wiggly", "Giga", "Chonky", "Shiny", "Angry", "Happy", "Soft", "Turbo",
|
||||||
|
}
|
||||||
|
things := []string{
|
||||||
|
"Potato", "Griefers", "Raccoons", "Pigeons", "Wizards", "Ninjas", "Pickles", "Dragons", "Goblins", "Burgers",
|
||||||
|
"Pancakes", "Hamsters", "Bananas", "Comets", "Robots", "Cats", "Kiwis", "Frogs", "Cupcakes", "Sprites",
|
||||||
|
}
|
||||||
|
verbs := []string{
|
||||||
|
"Zoom", "Bonk", "Yeet", "Vibe", "Hack", "Spark", "Bounce", "Nibble", "Smuggle", "Cook",
|
||||||
|
}
|
||||||
|
|
||||||
|
a := adjectives[randIndex(len(adjectives))]
|
||||||
|
v := verbs[randIndex(len(verbs))]
|
||||||
|
t := things[randIndex(len(things))]
|
||||||
|
return fmt.Sprintf("%d%s%s%s.zip", fileCount, a, v, t)
|
||||||
|
}
|
||||||
|
|
||||||
// UploadBundle zips multiple uploaded files into a single .zip stored on disk and tracked as one FileRecord.
|
// UploadBundle zips multiple uploaded files into a single .zip stored on disk and tracked as one FileRecord.
|
||||||
func (s *Service) UploadBundle(files []*multipart.FileHeader, deleteAfterDownload bool, expiresAfter time.Duration) (*FileRecord, error) {
|
func (s *Service) UploadBundle(files []*multipart.FileHeader, deleteAfterDownload bool, expiresAfter time.Duration) (*FileRecord, error) {
|
||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
@@ -105,7 +137,7 @@ func (s *Service) UploadBundle(files []*multipart.FileHeader, deleteAfterDownloa
|
|||||||
return nil, closeErr
|
return nil, closeErr
|
||||||
}
|
}
|
||||||
|
|
||||||
zipDisplayName := fmt.Sprintf("bundle-%d-files.zip", len(files))
|
zipDisplayName := cuteZipName(len(files))
|
||||||
|
|
||||||
f := &FileRecord{
|
f := &FileRecord{
|
||||||
ID: folderID,
|
ID: folderID,
|
||||||
|
|||||||
Reference in New Issue
Block a user