switched from h2non/filetype to garbiel-vasile/mimetype for determining file type
This commit is contained in:
		
							parent
							
								
									fca36e9b02
								
							
						
					
					
						commit
						2081998ca9
					
				
					 4 changed files with 24 additions and 21 deletions
				
			
		
							
								
								
									
										2
									
								
								go.mod
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
										
									
									
									
								
							| 
						 | 
					@ -17,6 +17,7 @@ require (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
require (
 | 
					require (
 | 
				
			||||||
	github.com/fsnotify/fsnotify v1.9.0 // indirect
 | 
						github.com/fsnotify/fsnotify v1.9.0 // indirect
 | 
				
			||||||
 | 
						github.com/gabriel-vasile/mimetype v1.4.9 // indirect
 | 
				
			||||||
	github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
 | 
						github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
 | 
				
			||||||
	github.com/jackc/pgpassfile v1.0.0 // indirect
 | 
						github.com/jackc/pgpassfile v1.0.0 // indirect
 | 
				
			||||||
	github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
 | 
						github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
 | 
				
			||||||
| 
						 | 
					@ -29,6 +30,7 @@ require (
 | 
				
			||||||
	github.com/spf13/pflag v1.0.6 // indirect
 | 
						github.com/spf13/pflag v1.0.6 // indirect
 | 
				
			||||||
	github.com/subosito/gotenv v1.6.0 // indirect
 | 
						github.com/subosito/gotenv v1.6.0 // indirect
 | 
				
			||||||
	go.uber.org/multierr v1.11.0 // indirect
 | 
						go.uber.org/multierr v1.11.0 // indirect
 | 
				
			||||||
 | 
						golang.org/x/net v0.39.0 // indirect
 | 
				
			||||||
	golang.org/x/sync v0.14.0 // indirect
 | 
						golang.org/x/sync v0.14.0 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.33.0 // indirect
 | 
						golang.org/x/sys v0.33.0 // indirect
 | 
				
			||||||
	golang.org/x/text v0.25.0 // indirect
 | 
						golang.org/x/text v0.25.0 // indirect
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										4
									
								
								go.sum
									
										
									
									
									
								
							
							
						
						
									
										4
									
								
								go.sum
									
										
									
									
									
								
							| 
						 | 
					@ -9,6 +9,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
 | 
				
			||||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
 | 
					github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
 | 
				
			||||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
 | 
					github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
 | 
				
			||||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
 | 
					github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
 | 
				
			||||||
 | 
					github.com/gabriel-vasile/mimetype v1.4.9 h1:5k+WDwEsD9eTLL8Tz3L0VnmVh9QxGjRmjBvAG7U/oYY=
 | 
				
			||||||
 | 
					github.com/gabriel-vasile/mimetype v1.4.9/go.mod h1:WnSQhFKJuBlRyLiKohA/2DtIlPFAbguNaG7QCHcyGok=
 | 
				
			||||||
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
 | 
					github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
 | 
				
			||||||
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
 | 
					github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
 | 
				
			||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
 | 
					github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
 | 
				
			||||||
| 
						 | 
					@ -66,6 +68,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
 | 
				
			||||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
 | 
					go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
 | 
				
			||||||
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
 | 
					golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
 | 
				
			||||||
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
 | 
					golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
 | 
				
			||||||
 | 
					golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
 | 
				
			||||||
 | 
					golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
 | 
				
			||||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
 | 
					golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
 | 
				
			||||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
 | 
					golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
 | 
				
			||||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
 | 
					golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -46,6 +46,7 @@ func setDefaults() {
 | 
				
			||||||
	viper.SetDefault("db.debug", false)
 | 
						viper.SetDefault("db.debug", false)
 | 
				
			||||||
	// Others
 | 
						// Others
 | 
				
			||||||
	viper.SetDefault("processing.oleurl", "http://localhost:5000")
 | 
						viper.SetDefault("processing.oleurl", "http://localhost:5000")
 | 
				
			||||||
 | 
					  viper.SetDefault("processing.maxmimesize", "0")
 | 
				
			||||||
	viper.SetDefault("store.path", "./storage/files/")
 | 
						viper.SetDefault("store.path", "./storage/files/")
 | 
				
			||||||
	viper.SetDefault("debug", false)
 | 
						viper.SetDefault("debug", false)
 | 
				
			||||||
  // UI Interface info
 | 
					  // UI Interface info
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,41 +1,37 @@
 | 
				
			||||||
package store
 | 
					package store
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"github.com/h2non/filetype"
 | 
					 | 
				
			||||||
	"log/slog"
 | 
						"log/slog"
 | 
				
			||||||
	"os"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/gabriel-vasile/mimetype"
 | 
				
			||||||
 | 
						"github.com/spf13/viper"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Returns the MIME type of a file
 | 
					// Returns the MIME type of a file
 | 
				
			||||||
func GetFileType(fileId string) (string, error) {
 | 
					func GetFileType(fileId string) (string, error) {
 | 
				
			||||||
 | 
					  start := time.Now()
 | 
				
			||||||
	path, err := AbsPath(fileId)
 | 
						path, err := AbsPath(fileId)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return "application/octet-stream", nil
 | 
							return "application/octet-stream", nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	file, err := os.Open(path)
 | 
					  mimetype.SetLimit(uint32(viper.GetSizeInBytes("processing.maxmimesize")))
 | 
				
			||||||
 | 
						mtype, err := mimetype.DetectFile(path)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		slog.Error("Storage not accessible", "error", err, "fileid", fileId)
 | 
							slog.Error("Storage not accessible", "error", err, "file-uuid", fileId, "mimetype", mtype.String())
 | 
				
			||||||
		return "application/octet-stream", err
 | 
							return "application/octet-stream", err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					  slog.Debug("store.GetFileType", "file-uuid", fileId, "mimetype", mtype.String(), "extension", mtype.Extension(), "duration", time.Now().Sub(start).String())
 | 
				
			||||||
	// We only have to pass the file header = first 261 bytes
 | 
					  return mtype.String(), nil
 | 
				
			||||||
	head := make([]byte, 261)
 | 
					 | 
				
			||||||
	file.Read(head)
 | 
					 | 
				
			||||||
	return GetBytesFileType(head)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Returns the MimeType for a []byte
 | 
					// Returns the MimeType for a []byte
 | 
				
			||||||
// We only have to pass the file header = first 261 bytes
 | 
					// Only pass the necessary data!
 | 
				
			||||||
func GetBytesFileType(data []byte) (string, error) {
 | 
					func GetBytesFileType(data []byte) (string) {
 | 
				
			||||||
	kind, err := filetype.Match(data)
 | 
					  mimetype.SetLimit(uint32(viper.GetSizeInBytes("processing.maxmimesize")))
 | 
				
			||||||
	slog.Debug("GetBytesFileType", "data", data, "file-mime", kind.MIME.Value)
 | 
					  mtype := mimetype.Detect(data)
 | 
				
			||||||
	if err != nil {
 | 
						slog.Debug("GetBytesFileType", "data", data, "file-mime", mtype.String())
 | 
				
			||||||
		slog.Error("Could not determine file type", "error", err)
 | 
					 | 
				
			||||||
		return "application/octet-stream", err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if kind == filetype.Unknown {
 | 
					 | 
				
			||||||
		return "application/octet-stream", nil
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return kind.MIME.Value, nil
 | 
					
 | 
				
			||||||
 | 
						return mtype.String()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue