package sendaround import ( "io" "net/http" "os" "path/filepath" ) func normalizeFilePath(filePath string) string { return filepath.ToSlash(filePath) } type RemoteFile interface { FileName() string Length() uint64 MimeType() string } type File interface { RemoteFile getReader() io.ReadSeeker } type fileRemote struct { fileName string length uint64 mimeType string } func (f *fileRemote) FileName() string { return f.fileName } func (f *fileRemote) Length() uint64 { return f.length } func (f *fileRemote) MimeType() string { return f.mimeType } type file struct { io.ReadSeeker fileName string length uint64 mimeType string } func (f *file) FileName() string { return f.fileName } func (f *file) Length() uint64 { return f.length } func (f *file) MimeType() string { return f.mimeType } func (f *file) getReader() io.ReadSeeker { return f.ReadSeeker } func FileFromFilesystem(f *os.File, newFileName string) (data File, err error) { retval := new(file) retval.ReadSeeker = f if len(newFileName) > 0 { retval.fileName = newFileName } else { retval.fileName = filepath.Base(f.Name()) } retval.fileName = normalizeFilePath(retval.fileName) fi, err := f.Stat() if err != nil { return } retval.length = uint64(fi.Size()) // Only the first 512 bytes are used to sniff the content type. buf := make([]byte, 512) _, err = f.Read(buf) if err != nil { return } f.Seek(0, os.SEEK_SET) retval.mimeType = http.DetectContentType(buf) data = retval return } func FileFromReader(fileName string, length uint64, mimeType string, r io.ReadSeeker) (data File) { if len(mimeType) <= 0 { mimeType = "application/octet-stream" } return &file{ ReadSeeker: r, fileName: normalizeFilePath(fileName), length: length, mimeType: mimeType, } }