mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-02 03:54:59 +08:00
40 lines
709 B
Go
40 lines
709 B
Go
package common
|
|
|
|
import (
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type Helpers struct {
|
|
AbsolutePath func(p string) string
|
|
MimeType func(p string) string
|
|
}
|
|
|
|
func NewHelpers(config *Config) *Helpers {
|
|
return &Helpers{
|
|
MimeType: mimeType(config),
|
|
AbsolutePath: absolutePath(config),
|
|
}
|
|
}
|
|
|
|
func absolutePath(c *Config) func(p string) string {
|
|
return func(p string) string {
|
|
return filepath.Join(c.Runtime.Dirname, p)
|
|
}
|
|
}
|
|
|
|
func mimeType(c *Config) func(p string) string {
|
|
return func(p string) string {
|
|
ext := filepath.Ext(p)
|
|
if ext != "" {
|
|
ext = ext[1:]
|
|
}
|
|
ext = strings.ToLower(ext)
|
|
mType := c.MimeTypes[ext]
|
|
if mType == "" {
|
|
return "application/octet-stream"
|
|
}
|
|
return mType
|
|
}
|
|
}
|