mirror of
https://github.com/criyle/go-judge.git
synced 2025-11-04 14:50:02 +08:00
33 lines
812 B
Go
33 lines
812 B
Go
package filestore
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/rand"
|
|
"encoding/base32"
|
|
|
|
"github.com/criyle/go-judge/file"
|
|
)
|
|
|
|
const randIDLength = 12
|
|
|
|
// FileStore defines interface to store file
|
|
type FileStore interface {
|
|
Add(string, []byte) (string, error) // Add creates a file with name & content to the storage, returns id
|
|
Remove(string) bool // Remove deletes a file by id
|
|
Get(string) file.File // Get file by id, nil if not exists
|
|
List() []string // List return all file ids
|
|
}
|
|
|
|
func generateID() (string, error) {
|
|
b := make([]byte, randIDLength)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if _, err := base32.NewEncoder(base32.StdEncoding, &buf).Write(b); err != nil {
|
|
return "", err
|
|
}
|
|
return string(buf.Bytes()), nil
|
|
}
|