Files
WarpBox/lib/server/presentation.go

112 lines
3.1 KiB
Go
Raw Normal View History

package server
import (
"fmt"
"io"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"warpbox/lib/models"
)
func decorateBoxFile(boxID string, file models.BoxFile) models.BoxFile {
if file.MimeType == "" {
file.MimeType = mimeTypeForFile(filepath.Join(boxPath(boxID), file.Name), file.Name)
}
if file.SizeLabel == "" {
file.SizeLabel = formatBytes(file.Size)
}
file.IconPath = iconForMimeType(file.MimeType, file.Name)
file.DownloadPath = "/box/" + boxID + "/files/" + url.PathEscape(file.Name)
file.UploadPath = "/box/" + boxID + "/files/" + url.PathEscape(file.ID) + "/upload"
file.IsComplete = file.Status == fileStatusReady
switch file.Status {
case fileStatusReady:
file.StatusLabel = "Ready"
file.Title = "Download " + file.Name
case fileStatusFailed:
file.StatusLabel = "Failed"
file.Title = "Failed to upload"
case fileStatusWork:
file.StatusLabel = "Loading"
file.Title = "Loading"
default:
file.Status = fileStatusWait
file.StatusLabel = "Waiting"
file.Title = "Loading"
}
return file
}
func mimeTypeForFile(path string, filename string) string {
if mimeType := mime.TypeByExtension(strings.ToLower(filepath.Ext(filename))); mimeType != "" {
return mimeType
}
file, err := os.Open(path)
if err != nil {
return "application/octet-stream"
}
defer file.Close()
buffer := make([]byte, 512)
bytesRead, err := file.Read(buffer)
if err != nil && err != io.EOF {
return "application/octet-stream"
}
return http.DetectContentType(buffer[:bytesRead])
}
func iconForMimeType(mimeType string, filename string) string {
extension := strings.ToLower(filepath.Ext(filename))
switch {
case extension == ".exe":
return "/static/img/icons/Program Files Icons - PNG/MSONSEXT.DLL_14_6-0.png"
case strings.HasPrefix(mimeType, "image/"):
return "/static/img/sprites/bitmap.png"
case strings.HasPrefix(mimeType, "video/"):
return "/static/img/icons/netshow_notransm-1.png"
case strings.HasPrefix(mimeType, "audio/"):
return "/static/img/icons/netshow_notransm-1.png"
case strings.HasPrefix(mimeType, "text/") || extension == ".md":
return "/static/img/sprites/notepad_file-1.png"
case strings.Contains(mimeType, "zip") || strings.Contains(mimeType, "compressed") || extension == ".rar" || extension == ".7z" || extension == ".tar" || extension == ".gz":
return "/static/img/icons/Windows Icons - PNG/zipfldr.dll_14_101-0.png"
case extension == ".ttf" || extension == ".otf" || extension == ".woff" || extension == ".woff2":
return "/static/img/sprites/font.png"
case extension == ".pdf":
return "/static/img/sprites/journal.png"
case extension == ".html" || extension == ".css" || extension == ".js":
return "/static/img/sprites/frame_web-0.png"
default:
return "/static/img/icons/Windows Icons - PNG/ole2.dll_14_DEFICON.png"
}
}
func formatBytes(bytes int64) string {
units := []string{"B", "KB", "MB", "GB"}
size := float64(bytes)
unitIndex := 0
for size >= 1024 && unitIndex < len(units)-1 {
size /= 1024
unitIndex++
}
if unitIndex == 0 {
return fmt.Sprintf("%d %s", bytes, units[unitIndex])
}
return fmt.Sprintf("%.1f %s", size, units[unitIndex])
}