feat: modularize the code.
This commit is contained in:
parent
6f4757a1e9
commit
a59c892087
7 changed files with 293 additions and 243 deletions
34
misc.go
Normal file
34
misc.go
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Miscelaneous functions
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
func CreateDirIfNotExisting(d string) {
|
||||
if _, err := os.Stat(d); errors.Is(err, os.ErrNotExist) {
|
||||
err := os.Mkdir(d, os.ModePerm)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the file content type
|
||||
func GetFileContentType(ouput *os.File) (string, error) {
|
||||
buf := make([]byte, 512)
|
||||
|
||||
_, err := ouput.Read(buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
contentType := http.DetectContentType(buf)
|
||||
|
||||
return contentType, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue