now reading mimetype, poorly
This commit is contained in:
+28
-2
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -42,11 +43,10 @@ func readFilesFromFileStorePath(fileStorePath string) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(fileStat.Size())
|
||||
|
||||
fileInfo, err := file.Info() // This returns an os.FileInfo
|
||||
if err != nil {
|
||||
continue
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// generate new StoredFileNode
|
||||
@@ -65,12 +65,38 @@ func readFilesFromFileStorePath(fileStorePath string) {
|
||||
newNode.IsFolder = true
|
||||
}
|
||||
|
||||
if !file.IsDir() {
|
||||
newNode.Extension = filepath.Ext(fullPath)
|
||||
|
||||
fileHandler, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer fileHandler.Close()
|
||||
|
||||
// Only need the first 512 bytes for mimetype detection
|
||||
buffer := make([]byte, 512)
|
||||
_, err = fileHandler.Read(buffer)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Always returns a valid MIME type, defaults to "application/octet-stream"
|
||||
newNode.MimeType = http.DetectContentType(buffer)
|
||||
}
|
||||
|
||||
shownNodes = append(shownNodes, newNode)
|
||||
}
|
||||
|
||||
fmt.Println(shownNodes)
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Lshortfile: prints "main.go:15"
|
||||
// Llongfile: prints the full path "/path/to/main.go:15"
|
||||
log.SetFlags(log.LstdFlags | log.Llongfile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user