Fix bug with files created in current dir

This commit is contained in:
Justin Hawkins 2024-06-04 08:38:39 +09:30
parent 1aaeecc8b9
commit 75027684ec

34
main.go
View File

@ -44,33 +44,36 @@ const defaultPathTemplate = "%s-%d-%d-%d.%s"
func main() { func main() {
t0 := time.Now() t0 := time.Now()
filenamePtr := flag.String("filename", "", "filename to open") var filename, baseName, outputFormat, pathTemplate string
tileSizePtr := flag.Int("tile-size", 256, "tile size, in pixels") var tileSize, concurrency int
concurrencyPtr := flag.Int("concurrency", 5, "how many tiles to generate concurrently (threads)")
baseName := flag.String("basename", "tile", "base of the output files") flag.StringVar(&filename, "filename", "", "filename to open")
outFormat := flag.String("format", "png", "output format (jpg/png)") flag.IntVar(&tileSize, "tile-size", 256, "tile size, in pixels")
pathTemplate := flag.String("path-template", defaultPathTemplate, "template for output files - base, zoom, x, y, format") flag.IntVar(&concurrency, "concurrency", 5, "how many tiles to generate concurrently (threads)")
flag.StringVar(&baseName, "basename", "tile", "base of the output files")
flag.StringVar(&outputFormat, "format", "png", "output format (jpg/png)")
flag.StringVar(&pathTemplate, "path-template", defaultPathTemplate, "template for output files - base, zoom, x, y, format")
flag.Parse() flag.Parse()
if *filenamePtr == "" { if filename == "" {
fmt.Println("Error: You must specify a filename with --filename") fmt.Println("Error: You must specify a filename with -filename")
return return
} }
if *outFormat != "jpg" && *outFormat != "png" { if outputFormat != "jpg" && outputFormat != "png" {
fmt.Println("Error: -format must be jpg or png") fmt.Println("Error: -format must be jpg or png")
return return
} }
log.Println("opening file:", *filenamePtr) log.Println("opening file:", filename)
src, err := os.Open(*filenamePtr) src, err := os.Open(filename)
if err != nil { if err != nil {
fmt.Println("Error: Could not open file:", err) fmt.Println("Error: Could not open file:", err)
return return
} }
processImage(src, *baseName, *pathTemplate, *outFormat, *tileSizePtr, *concurrencyPtr, diskOutput{}) processImage(src, baseName, pathTemplate, outputFormat, tileSize, concurrency, diskOutput{})
log.Printf("done in %.2f", time.Since(t0).Seconds()) log.Printf("done in %.2f", time.Since(t0).Seconds())
} }
@ -101,6 +104,7 @@ func processImage(input io.Reader, basename string, pathTemplate string, format
z := max_zoom z := max_zoom
log.Println("maximum zoom level is", max_zoom) log.Println("maximum zoom level is", max_zoom)
log.Println("starting tiling with concurrency of", concurrency) log.Println("starting tiling with concurrency of", concurrency)
log.Printf("basename '%s'", basename)
results := make(chan string) results := make(chan string)
rp := resultPrinter{ rp := resultPrinter{
@ -196,10 +200,14 @@ type diskOutput struct {
func (do diskOutput) CreatePathAndFile(fn string) (io.WriteCloser, error) { func (do diskOutput) CreatePathAndFile(fn string) (io.WriteCloser, error) {
dir, _ := filepath.Split(fn) dir, _ := filepath.Split(fn)
if dir == "" {
// no need to do anything, going in current dir
} else {
err := os.MkdirAll(dir, 0777) err := os.MkdirAll(dir, 0777)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
writer, err := os.Create(fn) writer, err := os.Create(fn)
return writer, err return writer, err
@ -223,9 +231,9 @@ func tileWorker(jobs <-chan tileJob, results chan<- string) {
output_filename := fmt.Sprintf(j.pathTemplate, j.baseName, j.zoom, j.x, j.y, j.format) output_filename := fmt.Sprintf(j.pathTemplate, j.baseName, j.zoom, j.x, j.y, j.format)
cropped := imaging.Crop(j.src, image.Rect(j.tileSizeX*j.x, j.tileSizeY*j.y, j.tileSizeX*j.x+j.tileSizeX, j.tileSizeY*j.y+j.tileSizeY)) cropped := imaging.Crop(j.src, image.Rect(j.tileSizeX*j.x, j.tileSizeY*j.y, j.tileSizeX*j.x+j.tileSizeX, j.tileSizeY*j.y+j.tileSizeY))
// log.Printf("writing to %s", output_filename)
writer, err := j.output.CreatePathAndFile(output_filename) writer, err := j.output.CreatePathAndFile(output_filename)
if err != nil { if err != nil {
log.Fatalf("could not create path/file: '%s'", err.Error())
panic(err) panic(err)
} }
if j.format == "png" { if j.format == "png" {