You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImagesBusiness.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Business;
  3. use App\Http\DBO\ImageUploadDbo;
  4. use Carbon\Carbon;
  5. use Exception;
  6. use Luticate\Auth\DBO\LuticateUsersDbo;
  7. use Luticate\Utils\LuBusiness;
  8. use App\Http\DataAccess\ImagesDataAccess;
  9. use App\Http\DBO\ImagesDbo;
  10. use Luticate\Utils\LuLog;
  11. class ImagesBusiness extends LuBusiness {
  12. const IMAGES_PATH = "../storage/images/";
  13. const IMAGES_FORMAT = "jpeg";
  14. protected static function getDataAccess()
  15. {
  16. return new ImagesDataAccess();
  17. }
  18. public static function generateImagePath(ImageUploadDbo $image, LuticateUsersDbo $_user)
  19. {
  20. return self::IMAGES_PATH . $_user->getId() . "-" . hash("sha256", $image->getName()) .
  21. "-" . time() . "." . self::IMAGES_FORMAT;
  22. }
  23. public static function getAllLite($only_mine, LuticateUsersDbo $_user, $page = 0, $perPage = 20000000)
  24. {
  25. return ImagesDataAccess::getAllLite($only_mine, $_user, $page, $perPage);
  26. }
  27. public static function getRawById($image_id)
  28. {
  29. /**
  30. * @var $dbo ImagesDbo
  31. */
  32. $dbo = self::getById($image_id);
  33. echo file_get_contents($dbo->getPath());
  34. exit;
  35. }
  36. public static function upload(ImageUploadDbo $image, LuticateUsersDbo $_user)
  37. {
  38. $path = self::generateImagePath($image, $_user);
  39. $dbo = new ImagesDbo();
  40. $dbo->setUserId($_user->getId());
  41. $dbo->setPath($path);
  42. $dbo->setDate(Carbon::now());
  43. $dbo->setName($image->getName());
  44. if (is_null($dbo->getName()) || $dbo->getName() === "") {
  45. $dbo->setName(Carbon::now()->toW3cString());
  46. }
  47. $image->getImage()->scaleImage(420, 420, true);
  48. $image->getImage()->setImageFormat(self::IMAGES_FORMAT);
  49. try
  50. {
  51. $image->getImage()->writeImage($path);
  52. return ImagesDataAccess::addId($dbo);
  53. }
  54. catch (Exception $e)
  55. {
  56. LuLog::log($e);
  57. try
  58. {
  59. unlink($path);
  60. }
  61. catch (Exception $e)
  62. {
  63. }
  64. abort(500, "Failed to save image");
  65. }
  66. return null;
  67. }
  68. public static function del($image_id, LuticateUsersDbo $_user)
  69. {
  70. /**
  71. * @var $imageDbo ImagesDbo
  72. */
  73. $imageDbo = self::getById($image_id);
  74. if ($imageDbo->getUserId() != $_user->getId()) {
  75. self::unauthorized("You do not own this image");
  76. }
  77. self::deleteById($imageDbo->getId());
  78. try
  79. {
  80. unlink($imageDbo->getPath());
  81. }
  82. catch (Exception $e)
  83. {
  84. LuLog::log($e);
  85. }
  86. return true;
  87. }
  88. }