-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored - split functionalities into structs and interfaces
- Loading branch information
Showing
5 changed files
with
142 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/labstack/gommon/log" | ||
"github.com/unidoc/unidoc/pdf/creator" | ||
) | ||
|
||
type mergable interface { | ||
MergeTo(c *creator.Creator) error | ||
} | ||
|
||
type source struct { | ||
path, sourceType, mime, ext string | ||
pages []int | ||
} | ||
|
||
// Initiate new source file from input argument | ||
func NewSource(input string) mergable { | ||
fileInputParts := strings.Split(input, "~") | ||
|
||
path := fileInputParts[0] | ||
f, err := os.Open(fileInputParts[0]) | ||
if err != nil { | ||
log.Fatal("Cannot read source file:", fileInputParts[0]) | ||
} | ||
|
||
defer f.Close() | ||
|
||
ext := filepath.Ext(f.Name()) | ||
mime, err := getMimeType(f) | ||
if err != nil { | ||
log.Fatal("Error in getting mime type of file:", fileInputParts[0]) | ||
} | ||
|
||
sourceType, err := getFileType(mime, ext) | ||
if err != nil { | ||
log.Fatal("Error : %s (%s)", err.Error(), path) | ||
} | ||
|
||
pages := []int{} | ||
if len(fileInputParts) > 1 { | ||
pages = parsePageNums(fileInputParts[1]) | ||
} | ||
|
||
source := source{path, sourceType, mime, ext, pages} | ||
|
||
var m mergable | ||
switch sourceType { | ||
case "image": | ||
m = ImgSource{source} | ||
case "pdf": | ||
m = PDFSource{source} | ||
} | ||
|
||
return m | ||
} | ||
|
||
func getFileType(mime, ext string) (string, error) { | ||
pdfExts := []string{".pdf", ".PDF"} | ||
imgExts := []string{".jpg", ".jpeg", ".gif", ".png", ".tiff", ".tif", ".JPG", ".JPEG", ".GIF", ".PNG", ".TIFF", ".TIF"} | ||
|
||
switch { | ||
case mime == "application/pdf": | ||
return "pdf", nil | ||
case mime[:6] == "image/": | ||
return "image", nil | ||
case mime == "application/octet-stream" && in_array(ext, pdfExts): | ||
return "pdf", nil | ||
case mime == "application/octet-stream" && in_array(ext, imgExts): | ||
return "image", nil | ||
} | ||
|
||
return "error", errors.New("Could not detect file type.") | ||
} | ||
|
||
func parsePageNums(pagesInput string) []int { | ||
pages := []int{} | ||
|
||
for _, e := range strings.Split(pagesInput, ",") { | ||
pageNo, err := strconv.Atoi(strings.Trim(e, " \n")) | ||
if err != nil { | ||
fmt.Errorf("Invalid format! Example of a file input with page numbers: path/to/abc.pdf~1,2,3,5,6") | ||
os.Exit(1) | ||
} | ||
pages = append(pages, pageNo) | ||
} | ||
|
||
return pages | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters