reworking errors + now using a working dir for our converter

This commit is contained in:
Julien Neuhart
2018-03-30 09:19:37 +02:00
parent e847c86baa
commit a882b7e5c5
16 changed files with 203 additions and 241 deletions

View File

@@ -1,22 +1,36 @@
package converter
import (
"fmt"
"net/http"
"os"
gfile "github.com/gulien/gotenberg/app/handlers/converter/file"
"github.com/gulien/gotenberg/app/handlers/converter/process"
ghttp "github.com/gulien/gotenberg/app/handlers/http"
"github.com/satori/go.uuid"
)
type Converter struct {
files []*gfile.File
resultFilesPaths []string
FinalFilePath string
files []*gfile.File
workingDir string
}
type NoFileToConvertError struct{}
func (e *NoFileToConvertError) Error() string {
return "There is no file to convert"
}
func NewConverter(r *http.Request, contentType ghttp.ContentType) (*Converter, error) {
c := &Converter{}
c := &Converter{
workingDir: fmt.Sprintf("./%s/", uuid.NewV4().String()),
}
if err := os.Mkdir(c.workingDir, 0666); err != nil {
return nil, err
}
switch contentType {
case ghttp.MultipartFormDataContentType:
@@ -28,7 +42,6 @@ func NewConverter(r *http.Request, contentType ghttp.ContentType) (*Converter, e
formData := r.MultipartForm
files, ok := formData.File["files"]
if !ok {
// TODO
return nil, nil
}
@@ -40,9 +53,8 @@ func NewConverter(r *http.Request, contentType ghttp.ContentType) (*Converter, e
defer file.Close()
f, err := gfile.NewFile(file)
f, err := gfile.NewFile(c.workingDir, file)
if err != nil {
// todo err
return nil, err
}
@@ -50,29 +62,28 @@ func NewConverter(r *http.Request, contentType ghttp.ContentType) (*Converter, e
}
break
default:
f, err := gfile.NewFile(r.Body)
f, err := gfile.NewFile(c.workingDir, r.Body)
if err != nil {
// todo err
return nil, err
}
c.files = append(c.files, f)
}
if len(c.files) == 0 {
return nil, &NoFileToConvertError{}
}
return c, nil
}
func (c *Converter) Convert() error {
if len(c.files) == 0 {
return &noFileToConvertError{}
}
func (c *Converter) Convert() (string, error) {
var filesPaths []string
for _, f := range c.files {
if f.Type != gfile.PDFType {
path, err := process.ExecConversion(f)
path, err := process.ExecConversion(c.workingDir, f)
if err != nil {
return err
return "", err
}
filesPaths = append(filesPaths, path)
@@ -81,37 +92,21 @@ func (c *Converter) Convert() error {
}
}
path, err := process.ExecMerge(filesPaths)
if err != nil {
return err
if len(filesPaths) == 0 {
return filesPaths[0], nil
}
c.resultFilesPaths = filesPaths
c.FinalFilePath = path
path, err := process.ExecMerge(c.workingDir, filesPaths)
if err != nil {
return "", err
}
return nil
return path, nil
}
func (c *Converter) Clear() error {
for _, f := range c.files {
err := os.Remove(f.Path)
if err != nil {
return err
}
}
for _, path := range c.resultFilesPaths {
err := os.Remove(path)
if err != nil {
return err
}
}
if c.FinalFilePath != "" {
err := os.Remove(c.FinalFilePath)
if err != nil {
return err
}
if err := os.RemoveAll(c.workingDir); err != nil {
return err
}
return nil

View File

@@ -1,7 +0,0 @@
package converter
type noFileToConvertError struct{}
func (e *noFileToConvertError) Error() string {
return "There is no file to convert"
}

View File

@@ -10,30 +10,22 @@ import (
"github.com/satori/go.uuid"
)
type (
File struct {
Type FileType
Path string
}
type File struct {
Type FileType
Path string
}
FileType string
FileExt string
)
type FileType uint32
const (
PDFType FileType = "PDF"
HTMLType FileType = "HTML"
OfficeType FileType = "Office"
PDFExt FileExt = ".pdf"
HTMLExt FileExt = ".html"
OfficeExt FileExt = ""
PDFType FileType = iota
HTMLType
OfficeType
)
func NewFile(r io.Reader) (*File, error) {
func NewFile(workingDir string, r io.Reader) (*File, error) {
f := &File{
Path: MakeFilePath(),
Path: MakeFilePath(workingDir),
}
file, err := os.Create(f.Path)
@@ -58,7 +50,7 @@ func NewFile(r io.Reader) (*File, error) {
f.Type = t
f, err = reworkFilePath(f)
f, err = reworkFilePath(workingDir, f)
if err != nil {
return nil, err
}
@@ -66,8 +58,8 @@ func NewFile(r io.Reader) (*File, error) {
return f, nil
}
func MakeFilePath() string {
return fmt.Sprintf("./%s", uuid.NewV4().String())
func MakeFilePath(workingDir string) string {
return fmt.Sprintf("%s%s", workingDir, uuid.NewV4().String())
}
var filesTypes = map[ghttp.ContentType]FileType{
@@ -77,36 +69,54 @@ var filesTypes = map[ghttp.ContentType]FileType{
ghttp.ZipContentType: OfficeType,
}
type fileTypeNotFound struct{}
func (e *fileTypeNotFound) Error() string {
return "The file type was not found for the given 'Content-Type'"
}
func findFileType(f *os.File) (FileType, error) {
ct, err := ghttp.SniffContentType(f)
if err != nil {
return "", err
return 999, err
}
t, ok := filesTypes[ct]
if !ok {
// TODO error
return "", nil
return 999, &fileTypeNotFound{}
}
return t, nil
}
type FileExt string
const (
PDFExt FileExt = ".pdf"
HTMLExt FileExt = ".html"
OfficeExt FileExt = ""
)
var filesExtensions = map[FileType]FileExt{
PDFType: PDFExt,
HTMLType: HTMLExt,
OfficeType: OfficeExt,
}
func reworkFilePath(f *File) (*File, error) {
type fileExtNotFound struct{}
func (e *fileExtNotFound) Error() string {
return "The file extension was not found for the given file type"
}
func reworkFilePath(workingDir string, f *File) (*File, error) {
ext, ok := filesExtensions[f.Type]
if !ok {
// TODO error
return nil, nil
return nil, &fileExtNotFound{}
}
if ext != OfficeExt {
newPath := fmt.Sprintf("./%s%s", MakeFilePath(), ext)
newPath := fmt.Sprintf("%s%s", MakeFilePath(workingDir), ext)
err := os.Rename(f.Path, newPath)
if err != nil {

View File

@@ -1,13 +0,0 @@
package process
type impossibleConversionError struct{}
func (e *impossibleConversionError) Error() string {
return "Impossible conversion"
}
type commandTimeoutError struct{}
func (e *commandTimeoutError) Error() string {
return "The command has reached timeout"
}

View File

@@ -17,19 +17,21 @@ func Load(config *config.CommandsConfig) {
commandsConfig = config
}
func Reset() {
commandsConfig = nil
}
type conversionData struct {
FilePath string
ResultFilePath string
}
func ExecConversion(file *gfile.File) (string, error) {
type impossibleConversionError struct{}
func (e *impossibleConversionError) Error() string {
return "Impossible conversion"
}
func ExecConversion(workingDir string, file *gfile.File) (string, error) {
cmdData := &conversionData{
FilePath: file.Path,
ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(), gfile.PDFExt),
ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(workingDir), gfile.PDFExt),
}
var (
@@ -68,10 +70,10 @@ type mergeData struct {
ResultFilePath string
}
func ExecMerge(filesPaths []string) (string, error) {
func ExecMerge(workingDir string, filesPaths []string) (string, error) {
cmdData := &mergeData{
FilesPaths: filesPaths,
ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(), gfile.PDFExt),
ResultFilePath: fmt.Sprintf("%s%s", gfile.MakeFilePath(workingDir), gfile.PDFExt),
}
cmdTemplate := commandsConfig.Merge.Template
@@ -90,8 +92,13 @@ func ExecMerge(filesPaths []string) (string, error) {
return cmdData.ResultFilePath, nil
}
type commandTimeoutError struct{}
func (e *commandTimeoutError) Error() string {
return "The command has reached timeout"
}
func execCommand(command string, timeout int) error {
// Wait for the process to finish or kill it after a timeout.
cmd := exec.Command("/bin/sh", "-c", command)
if err := cmd.Start(); err != nil {
return err
@@ -102,6 +109,7 @@ func execCommand(command string, timeout int) error {
done <- cmd.Wait()
}()
// wait for the process to finish or kill it after a timeout.
select {
case <-time.After(time.Duration(timeout) * time.Second):
if err := cmd.Process.Kill(); err != nil {