github.com/vulppine/fotoDen@v0.3.0/generator/batch.go (about)

     1  package generator
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/h2non/bimg"
    10  )
    11  
    12  // BatchOperationOnFiles takes two arguments, an array of file names, and a function that takes a string and an int.
    13  //
    14  // The string will be the file name, while the int will be the index of the file in the array.
    15  //
    16  // The function will iterate over every file name until the end of the array is reached,
    17  // passing the file name into the function.
    18  //
    19  // Also takes an int channel - it will output '1' on that channel for every file operated on.
    20  func BatchOperationOnFiles(files []string, fn func(string, int) error, ch chan int) error {
    21  	for i := 0; i < len(files); i++ {
    22  		ch <- 1
    23  		err := fn(files[i], i)
    24  		if err != nil {
    25  			fmt.Println("An error occurred during operation: ", err)
    26  		}
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  // BatchCopyFile copies a list of file string names to the current WorkingDirectory by index.
    33  // Returns an error if one occurs, otherwise nil.
    34  // Also preserves the current extension of the file. (This is due to a NeoCities Free restriction)
    35  func BatchCopyFile(files []string, directory string, ch chan int) error {
    36  	wd, _ := os.Getwd()
    37  	verbose("Attempting a batch copy from " + wd + " to " + directory)
    38  	batchCopyFile := func(file string, index int) error {
    39  		err := CopyFile(file, filepath.Join(directory, file))
    40  		if err != nil {
    41  			return err
    42  		}
    43  
    44  		return nil
    45  	}
    46  
    47  	err := BatchOperationOnFiles(files, batchCopyFile, ch)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	return nil
    53  }
    54  
    55  // BatchImageConversion resizes a set of images to thumbnail size and puts them into the given directory, as according to CurrentConfig.
    56  // Returns an error if one occurs, otherwise nil.
    57  func BatchImageConversion(files []string, prefix string, directory string, ScalingOptions ImageScale, ch chan int) error {
    58  	wd, _ := os.Getwd()
    59  	verbose("Generating thumbnails in " + wd + " and placing them in " + directory)
    60  	batchResizeImage := func(file string, index int) error {
    61  		err := ResizeImage(file, prefix+"_"+strings.Split(filepath.Base(file), ".")[0]+".jpg", ScalingOptions, directory, bimg.JPEG)
    62  		if err != nil && err != fmt.Errorf("skip") {
    63  			return err
    64  		}
    65  
    66  		return nil
    67  	}
    68  
    69  	err := BatchOperationOnFiles(files, batchResizeImage, ch)
    70  	if err != nil {
    71  		return err
    72  
    73  	}
    74  
    75  	return nil
    76  }
    77  
    78  // BatchImageMeta takes a string array of files, and a destination directory, and generates a JSON file
    79  // containing non-EXIF metadata (such as names and descriptions) of image files for fotoDen to process.
    80  func BatchImageMeta(files []string, directory string, ch chan int) error {
    81  	wd, _ := os.Getwd()
    82  	verbose("Writing image metadata templates from images in " + wd + "and placing them in " + directory)
    83  	batchImageMeta := func(file string, index int) error {
    84  		meta := new(ImageMeta)
    85  
    86  		err := meta.WriteImageMeta(directory, file)
    87  		if err != nil {
    88  			return err
    89  		}
    90  
    91  		return nil
    92  	}
    93  
    94  	err := BatchOperationOnFiles(files, batchImageMeta, ch)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	return nil
   100  }