239 lines
3.9 KiB
Go
239 lines
3.9 KiB
Go
// Algumas dessas classes sao absurdas mas quero ver até onde isso vai.
|
|
package model
|
|
|
|
import (
|
|
"fmt"
|
|
"mime/multipart"
|
|
"net/textproto"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"crypto/md5"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Filename string
|
|
|
|
type BoxURL struct {
|
|
url string
|
|
}
|
|
|
|
var boxURLCheck = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
|
|
|
|
func CheckBoxURL(url string) (BoxURL, error) {
|
|
if url == "" || !boxURLCheck.MatchString(url) {
|
|
return BoxURL{}, fmt.Errorf(`invalid url: "%s"`, url)
|
|
}
|
|
return BoxURL{url}, nil
|
|
}
|
|
|
|
func (b BoxURL) String() string {
|
|
return b.url
|
|
}
|
|
|
|
type FileMime string
|
|
|
|
type Markdown string
|
|
|
|
type MD5Checksum [md5.Size]byte
|
|
|
|
func (m MD5Checksum) String() string {
|
|
return fmt.Sprintf("%x", m[:])
|
|
}
|
|
|
|
type File struct {
|
|
id uuid.UUID
|
|
name string
|
|
size int64
|
|
createdAt time.Time
|
|
mime FileMime
|
|
checksum MD5Checksum
|
|
}
|
|
|
|
func NewFile(
|
|
id uuid.UUID,
|
|
name Filename,
|
|
size int64,
|
|
createdat time.Time,
|
|
mime FileMime,
|
|
checksum MD5Checksum,
|
|
) File {
|
|
return File{
|
|
id: id,
|
|
name: string(name),
|
|
size: size,
|
|
createdAt: createdat,
|
|
mime: mime,
|
|
checksum: checksum,
|
|
}
|
|
}
|
|
|
|
func (f *File) ID() uuid.UUID {
|
|
return f.id
|
|
}
|
|
|
|
func (f *File) Name() string {
|
|
return f.name
|
|
}
|
|
|
|
func (f *File) Size() int64 {
|
|
return f.size
|
|
}
|
|
|
|
func (f *File) CreatedAt() time.Time {
|
|
return f.createdAt
|
|
}
|
|
|
|
func (f *File) Mime() FileMime {
|
|
return f.mime
|
|
}
|
|
|
|
func (f *File) Checksum() MD5Checksum {
|
|
return f.checksum
|
|
}
|
|
|
|
type FileUploadContext struct {
|
|
id uuid.UUID
|
|
uploadedDate time.Time
|
|
size int64
|
|
filename Filename
|
|
mime FileMime
|
|
fileHeader *multipart.FileHeader
|
|
}
|
|
|
|
func NewFileUploadContext(
|
|
id uuid.UUID,
|
|
uploadedDate time.Time,
|
|
size int64,
|
|
filename Filename,
|
|
mime FileMime,
|
|
fileHeader *multipart.FileHeader,
|
|
) FileUploadContext {
|
|
|
|
return FileUploadContext{
|
|
id: id,
|
|
uploadedDate: uploadedDate,
|
|
size: size,
|
|
filename: filename,
|
|
mime: mime,
|
|
fileHeader: fileHeader,
|
|
}
|
|
}
|
|
|
|
func (f *FileUploadContext) ID() uuid.UUID {
|
|
return f.id
|
|
}
|
|
|
|
func (f *FileUploadContext) UploadedDate() time.Time {
|
|
return f.uploadedDate
|
|
}
|
|
|
|
func (f *FileUploadContext) Size() int64 {
|
|
return f.size
|
|
}
|
|
|
|
func (f *FileUploadContext) Filename() Filename {
|
|
return f.filename
|
|
}
|
|
|
|
func (f *FileUploadContext) Mime() FileMime {
|
|
return f.mime
|
|
}
|
|
|
|
func (f *FileUploadContext) FileHeader() *multipart.FileHeader {
|
|
return f.fileHeader
|
|
}
|
|
|
|
type Box struct {
|
|
id uuid.UUID
|
|
url BoxURL
|
|
header Markdown
|
|
private bool
|
|
moderation bool
|
|
createdAt time.Time
|
|
lastUpdatedAt time.Time
|
|
|
|
// FIXME: should be encoded in bcrypt
|
|
editCode string
|
|
}
|
|
|
|
func NewBox(
|
|
id uuid.UUID,
|
|
url BoxURL,
|
|
editCode string,
|
|
header Markdown,
|
|
private, moderation bool,
|
|
createdAt, lastUpdatedAt time.Time,
|
|
) Box {
|
|
return Box{
|
|
id: id,
|
|
url: url,
|
|
header: header,
|
|
editCode: editCode,
|
|
private: private,
|
|
moderation: moderation,
|
|
createdAt: createdAt,
|
|
lastUpdatedAt: lastUpdatedAt,
|
|
}
|
|
}
|
|
|
|
func (b Box) ID() uuid.UUID {
|
|
return b.id
|
|
}
|
|
|
|
func (b Box) Url() BoxURL {
|
|
return b.url
|
|
}
|
|
|
|
func (b Box) Header() Markdown {
|
|
return b.header
|
|
}
|
|
|
|
func (b Box) EditCode() string {
|
|
return b.editCode
|
|
}
|
|
|
|
func (b Box) Private() bool {
|
|
return b.private
|
|
}
|
|
|
|
func (b Box) Moderation() bool {
|
|
return b.moderation
|
|
}
|
|
|
|
func (b Box) CreatedAt() time.Time {
|
|
return b.createdAt
|
|
}
|
|
|
|
func (b Box) LastUpdatedAt() time.Time {
|
|
return b.lastUpdatedAt
|
|
}
|
|
|
|
func (m FileMime) IsImage() bool {
|
|
return strings.Contains(string(m), "image")
|
|
}
|
|
|
|
func (m FileMime) IsVideo() bool {
|
|
return strings.Contains(string(m), "video")
|
|
}
|
|
|
|
func (m FileMime) PrettyType() string {
|
|
switch {
|
|
case strings.Contains(string(m), "gif"):
|
|
return "gif"
|
|
case m.IsImage():
|
|
return "image"
|
|
case m.IsVideo():
|
|
return "video"
|
|
case strings.Contains(string(m), "pdf"):
|
|
return "pdf"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
func MimeFromHeader(header textproto.MIMEHeader) FileMime {
|
|
return FileMime(header.Get("Content-Type"))
|
|
}
|