all repos — homestead @ f7d8beffaa09ecf863996d29c72f508eb3952c84

Code for my website

shared/storage/file.go (view raw)

package storage

import (
	"io"
	"mime"
	"net/http"
	"os"
	"path/filepath"
	"time"
)

type File struct {
	Path         string
	FSPath       string
	LastModified time.Time
	Etag         string
	Title        string
	Encodings    map[string]*os.File

	contentType string
}

func (f *File) AvailableEncodings() []string {
	encs := make([]string, 0, len(f.Encodings))
	for enc := range f.Encodings {
		encs = append(encs, enc)
	}

	return encs
}

func (f *File) GetContentType() string {
	if f.contentType != "" {
		return f.contentType
	}

	ext := filepath.Ext(f.FSPath)
	if ext == "" {
		scent := make([]byte, 0, 512)
		_, err := f.Encodings["identity"].Read(scent)
		if err != nil && err != io.EOF {
			return ""
		}

		f.contentType = http.DetectContentType(scent)
	} else {
		f.contentType = mime.TypeByExtension(ext)
	}

	return f.contentType
}