Added resulting document name setting

This commit is contained in:
Dmitriy Shishkov 2023-11-28 23:35:27 +03:00
parent 98132b89b8
commit 8e69f8aaf6
Signed by: dm1sh
GPG Key ID: 027994B0AA357688
3 changed files with 26 additions and 13 deletions

View File

@ -6,21 +6,21 @@ import (
"time" "time"
) )
func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType) (resultingPath string, err error) { func ConvertToPDF(folderPath string, filesPaths []string, contType ContentType, name string) (resultingPath string, err error) {
switch contType { switch contType {
case Images: case Images:
return ImagesToPDF(folderPath, filesPaths) return ImagesToPDF(folderPath, filesPaths, name)
case Office: case Office:
return OfficeToPDF(folderPath, filesPaths) return OfficeToPDF(folderPath, filesPaths, name)
case Pdfs: case Pdfs:
return PdfToPDF(folderPath, filesPaths) 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) (resultingPath string, err error) { func ImagesToPDF(folderPath string, filesPaths []string, name string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "Images") resultingPath = createResultingPath(folderPath, "Images", name)
if err := runCommand("convert", append(filesPaths, resultingPath)...); err != nil { if err := runCommand("convert", append(filesPaths, resultingPath)...); err != nil {
return "", err return "", err
@ -29,8 +29,8 @@ func ImagesToPDF(folderPath string, filesPaths []string) (resultingPath string,
return resultingPath, nil return resultingPath, nil
} }
func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string, err error) { func OfficeToPDF(folderPath string, filesPaths []string, name string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "Office document") resultingPath = createResultingPath(folderPath, "Office document", name)
err = runCommand( err = runCommand(
"soffice", "--headless", "--nologo", "--nofirststartwizard", "soffice", "--headless", "--nologo", "--nofirststartwizard",
@ -48,8 +48,8 @@ func OfficeToPDF(folderPath string, filesPaths []string) (resultingPath string,
return resultingPath, nil return resultingPath, nil
} }
func PdfToPDF(folderPath string, filesPath []string) (resultingPath string, err error) { func PdfToPDF(folderPath string, filesPath []string, name string) (resultingPath string, err error) {
resultingPath = createResultingPath(folderPath, "PDFs") resultingPath = createResultingPath(folderPath, "PDFs", name)
err = runCommand("gs", append([]string{ err = runCommand("gs", append([]string{
"-sDEVICE=pdfwrite", "-dCompressFonts=true", "-dPDFSETTINGS=/ebook", "-sDEVICE=pdfwrite", "-dCompressFonts=true", "-dPDFSETTINGS=/ebook",
@ -63,8 +63,14 @@ func PdfToPDF(folderPath string, filesPath []string) (resultingPath string, err
return resultingPath, nil return resultingPath, nil
} }
func createResultingPath(folderPath string, suffix string) string { func createResultingPath(folderPath string, suffix string, name string) string {
return folderPath + "/" + time.Now().Format(time.ANSIC) + " " + suffix + ".pdf" 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,6 +7,7 @@
</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>

View File

@ -137,7 +137,13 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
return 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 { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return