func detectContentType(sample []byte) (string, error) { if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR); err != nil { return "", err } defer magicmime.Close() return magicmime.TypeByBuffer(sample) }
func (v *Vault) AddFromPath(path string, o ItemCreateOptions) (*Item, error) { ext := filepath.Ext(path) mime := mime.TypeByExtension(ext) if mime == "" { if err := magicmime.Open(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR); err != nil { return nil, err } mimetype, err := magicmime.TypeByFile(path) if err != nil { return nil, err } mime = mimetype magicmime.Close() } stat, err := os.Stat(path) if err != nil { return nil, err } file, err := os.Open(path) if err != nil { return nil, err } defer file.Close() o.Size = uint64(stat.Size()) if o.Mime == "" { o.Mime = mime } return v.Add(file, o) }