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:     "generate",
	Aliases: []string{"g"},
	Short:   "generate thumbails for prestashop images.",
	Long:    "generate thumbails for prestashop images",
	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, imgTypeLabel := range imgTypes {

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

			// load image types
			var imageTypes []*models.ImageType
			db.Where(imgTypeLabel + " = 1").Find(&imageTypes)

			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`
			}

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

			pp.Println("imageTypes", imageTypes)

			t := throttler.New(12, 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))
					extension := filepath.Ext(imagePath)

					log.Println("imagePath=", imagePath)

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

					for _, imageType := range imageTypes {

						destinationFilePath := filepath.Join(imagePrefixPath, fmt.Sprintf("%d-%s%s", r.IdImage, imageType.Name, extension))
						log.Println("destinationFilePath=", destinationFilePath)

						newImage := bimg.NewImage(buffer)

						mediaType := newImage.Type()

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

						bimgOpts.Height = int(imageType.Height)
						bimgOpts.Width = int(imageType.Width)

						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:   "ps2thumb",
	Short: "ps2thumb is an helper to generate thumbnails for a prestashop deploy.",
	Long:  `ps2thumb is an helper to generate thumbnails for a prestashop deploy.`,
}

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