Guest User

Untitled

a guest
Nov 18th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. <?php
  2.  
  3. namespace VendorBundle\Services;
  4.  
  5. use ProductBundle\Entity\Product;
  6. use VendorBundle\Util\S3;
  7. use phpseclib\Net\SFTP;
  8. use Doctrine\ORM\EntityManager;
  9. use ProductBundle\Entity\InventoryItem;
  10. use ProductBundle\Entity\Image;
  11.  
  12. class FtpService
  13. {
  14. private $ftp_host;
  15. private $ftp_username;
  16. private $ftp_password;
  17. private $em;
  18. private $s3;
  19. private $resizer;
  20.  
  21. public function __construct($ftp_host, $ftp_username, $ftp_password, EntityManager $em, $s3, $resizer)
  22. {
  23. $this->ftp_host = $ftp_host;
  24. $this->ftp_username = $ftp_username;
  25. $this->ftp_password = $ftp_password;
  26. $this->em = $em;
  27. $this->s3 = $s3;
  28. $this->resizer = $resizer;
  29. }
  30.  
  31. public function getConnection() {
  32. $sftp = new SFTP($this->ftp_host);
  33. $login = $sftp->login($this->ftp_username,$this->ftp_password);
  34. $dir = $sftp->chdir("/path/to/images");
  35. return $sftp;
  36. }
  37.  
  38. public function getImages($path) {
  39. $views = [ "Misc" , "Front" , "Back" , "Angle" , "3Q Front"];
  40. $sftp = new SFTP($this->ftp_host);
  41. $login = $sftp->login($this->ftp_username,$this->ftp_password);
  42. $dir = $sftp->chdir("/path/to/images" . $path . "/");
  43. $list = $sftp->nlist();
  44. $results = [];
  45. $matches = 0;
  46. foreach($list as $each) {
  47. $filename = explode(".",$each);
  48. if ($filename[1] !== "jpg")
  49. continue;
  50. $parsedFile = explode("-",$filename[0]);
  51. $invItem = $this->em->getRepository("ProductBundle:InventoryItem")->findOneBy(["serialNumber" => $parsedFile[0], "status" => 1]);
  52. if ($invItem !== null && strlen($parsedFile[1]) == 1) {
  53. $view = $parsedFile[1] - count($views) <= -1 ? $parsedFile[1] : 0;
  54. $matches++;
  55. $newImage = $this->em->getRepository("ProductBundle:Image")->findOneBy(["path" => $path . '-' .$each]) ? "no" : "yes";
  56. if (!in_array($invItem->getProduct()->getName(), array_keys($results)))
  57. $results[$invItem->getProduct()->getName()] = [];
  58. $results[$invItem->getProduct()->getName()][] = ["newImage" => $newImage, "serial" => $parsedFile[0], "view" => $views[$view], "filename" => $each];
  59. }
  60. }
  61.  
  62. return(["results" => $results,
  63. "total" => count($list)-2,
  64. "matches" => $matches
  65. ]);
  66. }
  67.  
  68. public function transferImageToS3($path, $imageData) {
  69. $sftp = $this->getConnection();
  70. $ftpPath = "/path/to/images/" . $path . "/";
  71. foreach($imageData as $data) {
  72. $data = explode(',',$data);
  73. $filename = $path . '-' . $data[3];
  74. $invItem = $this->em->getRepository("ProductBundle:InventoryItem")->findOneBy(["serialNumber" => $data[1]]);
  75. $image = new Image();
  76. $image->setPath($filename);
  77. $image->setView($data[2]);
  78. $image->setInventoryItem($invItem);
  79. if ($this->s3->checkS3FileExists($filename) == null) {
  80. $file = $sftp->get($ftpPath . $data[3]);
  81. if ($data[2] === "Full Front" )
  82. $file = $this->resizer->ReorientImage($file, 90);
  83. else if ($data[2] === "Full Back")
  84. $file = $this->resizer->ReorientImage($file, 270);
  85. $this->s3->putS3File($file,$filename, $mimeType = null);
  86. };
  87. //get correct image path
  88. $image->setFullPath($this->s3->checkS3FileExists($filename));
  89. $this->em->persist($image);
  90. }
  91. $this->em->flush();
  92. }
  93.  
  94. }
Add Comment
Please, Sign In to add comment