new feature: users are now able to provide more file types to convert by adding entries in their configuration file (#3)

* new feature: users are now able to provide more file types to convert by adding entries in their configuration file

* removing README blueprint: was not working correctly

* improving code coverage of config package
This commit is contained in:
Julien Neuhart
2018-05-03 17:18:55 +02:00
committed by GitHub
parent ef9801b1fd
commit 4997b73bac
31 changed files with 770 additions and 692 deletions

View File

@@ -7,70 +7,32 @@ import (
"os"
"path/filepath"
"github.com/thecodingmachine/gotenberg/app/config"
"github.com/satori/go.uuid"
)
// File represents a file which has been created
// from a request.
type File struct {
// Type is the kind of file.
Type Type
// Extension is the extension of the file.
Extension string
// Path is the file path.
Path string
}
// Type represents what kind of file we're dealing with.
type Type uint32
const (
// PDFType represents a... PDF file.
PDFType Type = iota
// MarkdownType represents a... Markdown file.
MarkdownType
// HTMLType represents an... HTML file.
HTMLType
// OfficeType represents an... Office document.
OfficeType
)
// filesTypes associates a file extension with its file kind counterpart.
var filesTypes = map[string]Type{
".pdf": PDFType,
".md": MarkdownType,
".htm": HTMLType,
".html": HTMLType,
".doc": OfficeType,
".docx": OfficeType,
".odt": OfficeType,
".xls": OfficeType,
".xlsx": OfficeType,
".ods": OfficeType,
".ppt": OfficeType,
".pptx": OfficeType,
".odp": OfficeType,
}
type fileTypeNotFoundError struct {
fileName string
}
func (e *fileTypeNotFoundError) Error() string {
return fmt.Sprintf("File type was not found for '%s'", e.fileName)
}
// 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, fileName string) (*File, error) {
ext := filepath.Ext(fileName)
t, ok := filesTypes[ext]
if !ok {
return nil, &fileTypeNotFoundError{fileName: fileName}
if _, err := config.GetCommand(ext); err != nil {
return nil, err
}
f := &File{
Path: MakeFilePath(workingDir, ext),
Type: t,
Extension: ext,
Path: MakeFilePath(workingDir, ext),
}
file, err := os.Create(f.Path)