Example #1
0
func (ra ReverseAlteration) Alter(ressource resource.Resource) (resource.Resource, error) {
	content, _ := ressource.Dump()

	for i := 0; i < len(content)/2; i++ {
		content[i], content[len(content)-1-i] = content[len(content)-1-i], content[i]
	}

	return resource.NewBytes(content), nil
}
Example #2
0
// Dump reads files contained in AssetPack.Input and dumps
// them in AssetPack.Output if they match AssetPack.Pattern.
// If some filters are defined in AssetPack.Filters, they will be
// applied before dumping the assets with the AssetPack.Dumper.
// If some filters are passed in the `filters` parameter, they will be applied just after
// filters in AssetPack.Filters.
func (ap AssetPack) Dump(filters []Filter) error {
	var r resource.Resource

	files, err := ap.InputFiles()
	if err != nil {
		return err
	}

	for _, filename := range files {
		c, err := ioutil.ReadFile(filename)
		if err != nil {
			return err
		}

		r = resource.NewBytes(c)

		for _, a := range ap.Alterations {
			r, err = a.Alter(r)
			if err != nil {
				return err
			}
		}

		output, err := ap.OutputFile(filename, "")
		for _, f := range filters {
			if f.Pattern.Match(output) {
				r, err = f.Alteration.Alter(r)
				if err != nil {
					return err
				}
			}
		}

		c, err = r.Dump()
		if err != nil {
			return err
		}

		md5Output, err := ap.OutputFile(filename, "."+helpers.MD5(c))
		if err != nil {
			return err
		}

		err = ap.Dumper.Dump(md5Output, output, c)
		if err != nil {
			return err
		}
	}

	return nil
}
Example #3
0
// Alter runs optipng on a resource returns a one.
func (jpegOptim JpegOptim) Alter(r resource.Resource) (resource.Resource, error) {
	// create temporary file
	f, err := helpers.TempFile("", "statix_filter_", ".jpg")
	if err != nil {
		return &resource.Empty{}, err
	}
	defer func() {
		f.Close()
		os.Remove(f.Name())
	}()

	c, err := r.Dump()
	if err != nil {
		return &resource.Empty{}, err
	}

	_, err = f.Write(c)
	if err != nil {
		return &resource.Empty{}, err
	}

	// execute command
	var command *exec.Cmd
	bufOut := bytes.NewBuffer(nil)
	bufErr := bytes.NewBuffer(nil)
	if jpegOptim.StripAll {
		command = exec.Command(jpegOptim.Bin, "-m", strconv.Itoa(jpegOptim.Max), f.Name())
	} else {
		command = exec.Command(jpegOptim.Bin, "--strip-all", "-m", strconv.Itoa(jpegOptim.Max), f.Name())
	}
	command.Stdout = bufOut
	command.Stderr = bufErr
	err = command.Run()
	if err != nil {
		return &resource.Empty{}, fmt.Errorf("command error:\n%s", bufErr.String())
	}

	// read result
	c, err = ioutil.ReadFile(f.Name())
	if err != nil {
		return &resource.Empty{}, err
	}
	return resource.NewBytes(c), nil
}