Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace VendorBundle\Services;
- use ProductBundle\Entity\Product;
- use VendorBundle\Util\S3;
- use phpseclib\Net\SFTP;
- use Doctrine\ORM\EntityManager;
- use ProductBundle\Entity\InventoryItem;
- use ProductBundle\Entity\Image;
- class FtpService
- {
- private $ftp_host;
- private $ftp_username;
- private $ftp_password;
- private $em;
- private $s3;
- private $resizer;
- public function __construct($ftp_host, $ftp_username, $ftp_password, EntityManager $em, $s3, $resizer)
- {
- $this->ftp_host = $ftp_host;
- $this->ftp_username = $ftp_username;
- $this->ftp_password = $ftp_password;
- $this->em = $em;
- $this->s3 = $s3;
- $this->resizer = $resizer;
- }
- public function getConnection() {
- $sftp = new SFTP($this->ftp_host);
- $login = $sftp->login($this->ftp_username,$this->ftp_password);
- $dir = $sftp->chdir("/path/to/images");
- return $sftp;
- }
- public function getImages($path) {
- $views = [ "Misc" , "Front" , "Back" , "Angle" , "3Q Front"];
- $sftp = new SFTP($this->ftp_host);
- $login = $sftp->login($this->ftp_username,$this->ftp_password);
- $dir = $sftp->chdir("/path/to/images" . $path . "/");
- $list = $sftp->nlist();
- $results = [];
- $matches = 0;
- foreach($list as $each) {
- $filename = explode(".",$each);
- if ($filename[1] !== "jpg")
- continue;
- $parsedFile = explode("-",$filename[0]);
- $invItem = $this->em->getRepository("ProductBundle:InventoryItem")->findOneBy(["serialNumber" => $parsedFile[0], "status" => 1]);
- if ($invItem !== null && strlen($parsedFile[1]) == 1) {
- $view = $parsedFile[1] - count($views) <= -1 ? $parsedFile[1] : 0;
- $matches++;
- $newImage = $this->em->getRepository("ProductBundle:Image")->findOneBy(["path" => $path . '-' .$each]) ? "no" : "yes";
- if (!in_array($invItem->getProduct()->getName(), array_keys($results)))
- $results[$invItem->getProduct()->getName()] = [];
- $results[$invItem->getProduct()->getName()][] = ["newImage" => $newImage, "serial" => $parsedFile[0], "view" => $views[$view], "filename" => $each];
- }
- }
- return(["results" => $results,
- "total" => count($list)-2,
- "matches" => $matches
- ]);
- }
- public function transferImageToS3($path, $imageData) {
- $sftp = $this->getConnection();
- $ftpPath = "/path/to/images/" . $path . "/";
- foreach($imageData as $data) {
- $data = explode(',',$data);
- $filename = $path . '-' . $data[3];
- $invItem = $this->em->getRepository("ProductBundle:InventoryItem")->findOneBy(["serialNumber" => $data[1]]);
- $image = new Image();
- $image->setPath($filename);
- $image->setView($data[2]);
- $image->setInventoryItem($invItem);
- if ($this->s3->checkS3FileExists($filename) == null) {
- $file = $sftp->get($ftpPath . $data[3]);
- if ($data[2] === "Full Front" )
- $file = $this->resizer->ReorientImage($file, 90);
- else if ($data[2] === "Full Back")
- $file = $this->resizer->ReorientImage($file, 270);
- $this->s3->putS3File($file,$filename, $mimeType = null);
- };
- //get correct image path
- $image->setFullPath($this->s3->checkS3FileExists($filename));
- $this->em->persist($image);
- }
- $this->em->flush();
- }
- }
Add Comment
Please, Sign In to add comment