Compare commits

...

2 Commits

Author SHA1 Message Date
8e69f8aaf6
Added resulting document name setting 2023-11-28 23:35:27 +03:00
98132b89b8
Added ghostscript pdf compression and merging 2023-11-28 23:20:58 +03:00
3 changed files with 51 additions and 18 deletions

View File

@ -6,19 +6,21 @@ import (
"time"
)
func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType) (resultingPath string, error error) {
func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType, name string) (resultingPath string, err error) {
switch contType {
case Images:
return ImagesToPDF(folderPath, filesPaths)
return ImagesToPDF(folderPath, filesPaths, name)
case Office:
return OfficeToPDF(folderPath, filesPaths)
return OfficeToPDF(folderPath, filesPaths, name)
case Pdfs:
return PdfToPDF(folderPath, filesPaths, name)
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) (resultingPath string, error error) {
resultingPath = createResultingPath(folderPath, "Images")
func ImagesToPDF(folderPath string, filesPaths []string, name string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "Images", name)
if err := runCommand("convert", append(filesPaths, resultingPath)...); err != nil {
return "", err
@ -27,10 +29,10 @@ func ImagesToPDF(folderPath string, filesPaths []string) (resultingPath string,
return resultingPath, nil
}
func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string, error error) {
resultingPath = createResultingPath(folderPath, "Office document")
func OfficeToPDF(folderPath string, filesPaths []string, name string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "Office document", name)
err := runCommand(
err = runCommand(
"soffice", "--headless", "--nologo", "--nofirststartwizard",
"--norestore", "--convert-to", "pdf", "--outdir", folderPath, filesPaths[0],
)
@ -38,7 +40,7 @@ func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string,
return "", err
}
err = runCommand("mv", folderPath + "/" + "0.pdf", resultingPath)
err = runCommand("mv", folderPath+"/"+"0.pdf", resultingPath)
if err != nil {
return "", err
}
@ -46,8 +48,29 @@ func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string,
return resultingPath, nil
}
func createResultingPath(folderPath string, suffix string) string {
return folderPath + "/" + time.Now().Format(time.ANSIC) + " " + suffix + ".pdf"
func PdfToPDF(folderPath string, filesPath []string, name string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "PDFs", name)
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 {

View File

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

21
main.go
View File

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