Compare commits

..

No commits in common. "8e69f8aaf6a5a0e5b67cafc1f1f3b8a1210eb904" and "cd5b65a8867e6316828cae2883268b8dc0ef4b45" have entirely different histories.

3 changed files with 18 additions and 51 deletions

View File

@ -6,21 +6,19 @@ import (
"time" "time"
) )
func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType, name string) (resultingPath string, err error) { func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType) (resultingPath string, error error) {
switch contType { switch contType {
case Images: case Images:
return ImagesToPDF(folderPath, filesPaths, name) return ImagesToPDF(folderPath, filesPaths)
case Office: case Office:
return OfficeToPDF(folderPath, filesPaths, name) return OfficeToPDF(folderPath, filesPaths)
case Pdfs:
return PdfToPDF(folderPath, filesPaths, name)
default: default:
return "", fmt.Errorf("unhandled ContentType with %d index", contType) return "", fmt.Errorf("Unhandled ContentType with %d index", contType)
} }
} }
func ImagesToPDF(folderPath string, filesPaths []string, name string) (resultingPath string, err error) { func ImagesToPDF(folderPath string, filesPaths []string) (resultingPath string, error error) {
resultingPath = createResultingPath(folderPath, "Images", name) resultingPath = createResultingPath(folderPath, "Images")
if err := runCommand("convert", append(filesPaths, resultingPath)...); err != nil { if err := runCommand("convert", append(filesPaths, resultingPath)...); err != nil {
return "", err return "", err
@ -29,10 +27,10 @@ func ImagesToPDF(folderPath string, filesPaths []string, name string) (resulting
return resultingPath, nil return resultingPath, nil
} }
func OfficeToPDF(folderPath string, filesPaths []string, name string) (resultingPath string, err error) { func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string, error error) {
resultingPath = createResultingPath(folderPath, "Office document", name) resultingPath = createResultingPath(folderPath, "Office document")
err = runCommand( err := runCommand(
"soffice", "--headless", "--nologo", "--nofirststartwizard", "soffice", "--headless", "--nologo", "--nofirststartwizard",
"--norestore", "--convert-to", "pdf", "--outdir", folderPath, filesPaths[0], "--norestore", "--convert-to", "pdf", "--outdir", folderPath, filesPaths[0],
) )
@ -48,29 +46,8 @@ func OfficeToPDF(folderPath string, filesPaths []string, name string) (resulting
return resultingPath, nil return resultingPath, nil
} }
func PdfToPDF(folderPath string, filesPath []string, name string) (resultingPath string, err error) { func createResultingPath(folderPath string, suffix string) string {
resultingPath = createResultingPath(folderPath, "PDFs", name) return folderPath + "/" + time.Now().Format(time.ANSIC) + " " + suffix + ".pdf"
err = runCommand("gs", append([]string{
"-sDEVICE=pdfwrite", "-dCompressFonts=true", "-dPDFSETTINGS=/ebook",
"-dNOPAUSE", "-dQUIET", "-dBATCH", "-sOutputFile=" + resultingPath,
}, filesPath...)...)
if err != nil {
return "", err
}
return resultingPath, nil
}
func createResultingPath(folderPath string, suffix string, name string) string {
filename := time.Now().Format(time.ANSIC) + " " + suffix + ".pdf"
if len(name) > 0 {
filename = name + ".pdf"
}
return folderPath + "/" + filename
} }
func runCommand(command string, arg ...string) error { func runCommand(command string, arg ...string) error {

View File

@ -7,7 +7,6 @@
</head> </head>
<body> <body>
<form action="/upload" method="POST" enctype="multipart/form-data"> <form action="/upload" method="POST" enctype="multipart/form-data">
<input placeholder="optional" type="text" name="filename" id="filename">
<input type="file" name="files" multiple> <input type="file" name="files" multiple>
<button type="submit">Submit</button> <button type="submit">Submit</button>
</form> </form>

19
main.go
View File

@ -30,7 +30,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
const UPLOADS_FOLDER_PREFIX = "./uploads/" const UPLOADS_FOLDER_PREFIX = "./uploads/"
func saveFilesOnDisk(files []*multipart.FileHeader) (folderPath string, filesPaths []string, err error) { func saveFilesOnDisk(files []*multipart.FileHeader) (folderPath string, filesPaths []string, error error) {
// Create folder for uploaded files // Create folder for uploaded files
folderPath = UPLOADS_FOLDER_PREFIX + strconv.FormatInt(time.Now().Unix(), 10) folderPath = UPLOADS_FOLDER_PREFIX + strconv.FormatInt(time.Now().Unix(), 10)
@ -78,10 +78,9 @@ type ContentType int
const ( const (
Images ContentType = iota Images ContentType = iota
Office Office
Pdfs
) )
func detectContentType(files []*multipart.FileHeader) (contentType ContentType, err error) { func detectContentType(files []*multipart.FileHeader) (contentType ContentType, error error) {
for _, file := range files { for _, file := range files {
fileExt := strings.ToLower(filepath.Ext(file.Filename)) fileExt := strings.ToLower(filepath.Ext(file.Filename))
switch fileExt { switch fileExt {
@ -89,12 +88,10 @@ func detectContentType(files []*multipart.FileHeader) (contentType ContentType,
if len(files) == 1 { if len(files) == 1 {
return Office, nil return Office, nil
} else { } else {
return contentType, fmt.Errorf("expected one document, got %d", len(files)) return contentType, fmt.Errorf("Expected one document, got %d", len(files))
} }
case ".jpg", ".jpeg", ".png": case ".jpg", ".jpeg", ".png", ".pdf":
contentType = Images contentType = Images
case ".pdf":
contentType = Pdfs
default: default:
return contentType, fmt.Errorf("%s file extension is unsupported yet", fileExt) return contentType, fmt.Errorf("%s file extension is unsupported yet", fileExt)
} }
@ -137,13 +134,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
resultFileName := "" resFile, err := ConvertToPDF(folderPath, filesPaths, contType)
if len(r.MultipartForm.Value["filename"]) > 0 {
resultFileName = r.MultipartForm.Value["filename"][0]
}
resFile, err := ConvertToPDF(folderPath, filesPaths, contType, resultFileName)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return