godoc for file package + renaming some of its struct

This commit is contained in:
Julien Neuhart
2018-04-03 11:27:04 +02:00
parent 6206452e39
commit c440aa2957

View File

@@ -1,3 +1,4 @@
// Package file implements a solution for handling files coming from a request.
package file package file
import ( import (
@@ -10,19 +11,27 @@ import (
"github.com/satori/go.uuid" "github.com/satori/go.uuid"
) )
// File represents a file which has been created
// from a request.
type File struct { type File struct {
Type FileType Type Type
Path string Path string
} }
type FileType uint32 // Type represents what kind of file we're dealing with.
type Type uint32
const ( const (
PDFType FileType = iota // PDFType represents a... PDF file.
PDFType Type = iota
// HTMLType represents an... HTML file.
HTMLType HTMLType
// OfficeType represents an... Office document.
OfficeType OfficeType
) )
// NewFile creates a file in the considered directory.
// Returns a *File instance or an error if something bad happened.
func NewFile(workingDir string, r io.Reader) (*File, error) { func NewFile(workingDir string, r io.Reader) (*File, error) {
f := &File{ f := &File{
Path: MakeFilePath(workingDir), Path: MakeFilePath(workingDir),
@@ -58,11 +67,14 @@ func NewFile(workingDir string, r io.Reader) (*File, error) {
return f, nil return f, nil
} }
// MakeFilePath is a simple helper which generates a random file name
// and associates it with the considered directory to make a path.
func MakeFilePath(workingDir string) string { func MakeFilePath(workingDir string) string {
return fmt.Sprintf("%s%s", workingDir, uuid.NewV4().String()) return fmt.Sprintf("%s%s", workingDir, uuid.NewV4().String())
} }
var filesTypes = map[ghttp.ContentType]FileType{ // filesTypes associates a content type with its file kind counterpart.
var filesTypes = map[ghttp.ContentType]Type{
ghttp.PDFContentType: PDFType, ghttp.PDFContentType: PDFType,
ghttp.HTMLContentType: HTMLType, ghttp.HTMLContentType: HTMLType,
ghttp.OctetStreamContentType: OfficeType, ghttp.OctetStreamContentType: OfficeType,
@@ -75,7 +87,8 @@ func (e *fileTypeNotFound) Error() string {
return "The file type was not found for the given 'Content-Type'" return "The file type was not found for the given 'Content-Type'"
} }
func findFileType(f *os.File) (FileType, error) { // findFileType tries to detect what kind of file is the given file.
func findFileType(f *os.File) (Type, error) {
ct, err := ghttp.SniffContentType(f) ct, err := ghttp.SniffContentType(f)
if err != nil { if err != nil {
return 999, err return 999, err
@@ -89,15 +102,21 @@ func findFileType(f *os.File) (FileType, error) {
return t, nil return t, nil
} }
type FileExt string // Ext represents a file extension.
type Ext string
const ( const (
PDFExt FileExt = ".pdf" // PDFExt represents a... PDF extension.
HTMLExt FileExt = ".html" PDFExt Ext = ".pdf"
OfficeExt FileExt = "" // HTMLExt represents an... HTML extension.
HTMLExt Ext = ".html"
// OfficeExt is a empty string, as Office documents
// have a lot of different extensions (.docx, .doc and so on).
OfficeExt Ext = ""
) )
var filesExtensions = map[FileType]FileExt{ // filesExtensions associates a kind of file with its extension.
var filesExtensions = map[Type]Ext{
PDFType: PDFExt, PDFType: PDFExt,
HTMLType: HTMLExt, HTMLType: HTMLExt,
OfficeType: OfficeExt, OfficeType: OfficeExt,
@@ -109,6 +128,7 @@ func (e *fileExtNotFound) Error() string {
return "The file extension was not found for the given file type" return "The file extension was not found for the given file type"
} }
// reworkFilePath renames a file in the considered directory and adds its extension.
func reworkFilePath(workingDir string, f *File) (*File, error) { func reworkFilePath(workingDir string, f *File) (*File, error) {
ext, ok := filesExtensions[f.Type] ext, ok := filesExtensions[f.Type]
if !ok { if !ok {