You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
692 B
44 lines
692 B
3 years ago
|
package fs
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
// A struct that represents the main file information
|
||
|
type File struct {
|
||
|
Name string
|
||
|
Path string
|
||
|
ParentPath string
|
||
|
Size uint64
|
||
|
}
|
||
|
|
||
|
var ErrorNotFile error = fmt.Errorf("not a file")
|
||
|
|
||
|
func GetFile(path string) (*File, error) {
|
||
|
absPath, err := filepath.Abs(path)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
stats, err := os.Stat(absPath)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// check if it is a directory
|
||
|
if stats.IsDir() {
|
||
|
return nil, ErrorNotFile
|
||
|
}
|
||
|
|
||
|
file := File{
|
||
|
Name: stats.Name(),
|
||
|
Path: absPath,
|
||
|
ParentPath: filepath.Dir(absPath),
|
||
|
Size: uint64(stats.Size()),
|
||
|
}
|
||
|
|
||
|
return &file, nil
|
||
|
}
|