cmd

package
v0.0.0-...-d10ae67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ImportCmd = &cobra.Command{
	Use:     "resize",
	Aliases: []string{"r"},
	Short:   "resize too big images in prestashop images directory.",
	Long:    "resize too big images in prestashop images directory",
	Run: func(cmd *cobra.Command, args []string) {

		if !dryRun {
			var err error

			dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&collation=utf8mb4_unicode_ci&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName)
			db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
				NamingStrategy: schema.NamingStrategy{
					TablePrefix:   dbTablePrefix,
					SingularTable: true,
					NameReplacer:  strings.NewReplacer("ID", "Id"),
				},
			})
			if err != nil {
				log.Fatal(err)
			}

		}

		type Result struct {
			IdImage uint
		}

		bimgOpts := bimg.Options{
			Lossless: true,
			Quality:  imgQuality,
		}

		for imgType := range imgTypes {

			imgDir := filepath.Join(psDir, "img", imgType)

			limit := 10000
			offset := 0

			for {

				var query string
				switch imgType {
				case "products", "p":
					query = `SELECT image_shop.id_image  AS id_image
			FROM  eg_product p
			 LEFT JOIN eg_shop shop ON (shop.id_shop = 1)
			 LEFT JOIN eg_image_shop image_shop ON (image_shop.id_product = p.id_product AND image_shop.cover = 1 AND image_shop.id_shop = 1)
			 LEFT JOIN eg_image i ON (i.id_image = image_shop.id_image)
			WHERE (1 AND state = 1)
			GROUP BY p.id_product
			ORDER BY p.id_product DESC
			LIMIT {{ .offset }}, {{ .limit }}`
				}

				sqlResult := bytes.NewBufferString("")
				sqlTemplate, _ := template.New("").Parse(query)
				sqlTemplate.Execute(sqlResult, map[string]string{"offset": fmt.Sprintf("%d", offset), "limit": fmt.Sprintf("%d", limit)})

				// todo. load image list or parse them by name ?
				var results []*Result
				db.Debug().Raw(sqlResult.String()).Scan(&results)

				offset = offset + limit

				if len(results) == 0 {
					break
				}

				t := throttler.New(10, len(results))

				for _, result := range results {

					go func(r *Result) error {

						defer t.Done(nil)

						subDirectories := []rune(fmt.Sprintf("%d", r.IdImage))
						var prefixPath string
						for _, subDirectory := range subDirectories {
							prefixPath = filepath.Join(prefixPath, fmt.Sprintf("%c", subDirectory))
						}
						imagePrefixPath := filepath.Join(imgDir, prefixPath)
						imagePath := filepath.Join(imagePrefixPath, fmt.Sprintf("%d.jpg", r.IdImage))

						log.Println("imagePath=", imagePath)

						buffer, err := bimg.Read(imagePath)
						if err != nil {
							return err
						}

						destinationFilePath := imagePath
						log.Println("destinationFilePath=", destinationFilePath)

						newImage := bimg.NewImage(buffer)

						mediaType := newImage.Type()

						switch mediaType {
						case "unknown":
							log.Println("mediaType=", mediaType)
							return nil
						}

						if imgHeight > 0 {
							bimgOpts.Height = imgHeight
						}
						if imgWidth > 0 {
							bimgOpts.Width = imgWidth
						}

						newBytes, err := newImage.Process(bimgOpts)
						if err != nil {
							log.Warnln("newBytes.err=", err)
							return err
						}

						err = bimg.Write(destinationFilePath, newBytes)
						if err != nil {
							log.Warnln("Write.err=", err)
							return err
						}

						return nil
					}(result)
					t.Throttle()
				}
			}
		}
	},
}
View Source
var RootCmd = &cobra.Command{
	Use:   "ps2resize",
	Short: "ps2resize is an helper to resize prestashop images to define width or height with keep ratio.",
	Long:  `ps2resize is an helper to resize prestashop images to define width or height with keep ratio.`,
}

RootCmd is the root command for ovh-qa

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL