|
@@ -3,18 +3,65 @@
|
3
|
3
|
namespace App\Http\Business;
|
4
|
4
|
|
5
|
5
|
use App\Http\DBO\ImageUploadDbo;
|
|
6
|
+use Exception;
|
|
7
|
+use Luticate\Auth\DBO\LuticateUsersDbo;
|
6
|
8
|
use Luticate\Utils\LuBusiness;
|
7
|
9
|
use App\Http\DataAccess\ImagesDataAccess;
|
8
|
10
|
use App\Http\DBO\ImagesDbo;
|
9
|
11
|
|
10
|
12
|
class ImagesBusiness extends LuBusiness {
|
|
13
|
+
|
|
14
|
+ const IMAGES_PATH = "../storage/images/";
|
|
15
|
+
|
|
16
|
+ const IMAGES_FORMAT = "jpeg";
|
|
17
|
+
|
11
|
18
|
protected static function getDataAccess()
|
12
|
19
|
{
|
13
|
20
|
return new ImagesDataAccess();
|
14
|
21
|
}
|
15
|
22
|
|
16
|
|
- public static function upload(ImageUploadDbo $image)
|
|
23
|
+ public static function generateImagePath(ImageUploadDbo $image, LuticateUsersDbo $_user)
|
|
24
|
+ {
|
|
25
|
+ return self::IMAGES_PATH . $_user->getId() . "-" . hash("sha256", $image->getName()) .
|
|
26
|
+ "-" . time() . "." . self::IMAGES_FORMAT;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ public static function upload(ImageUploadDbo $image, LuticateUsersDbo $_user)
|
|
30
|
+ {
|
|
31
|
+ $path = self::generateImagePath($image, $_user);
|
|
32
|
+
|
|
33
|
+ $dbo = new ImagesDbo();
|
|
34
|
+ $dbo->setUserId($_user->getId());
|
|
35
|
+ $dbo->setPath($path);
|
|
36
|
+
|
|
37
|
+ $image->getImage()->scaleImage(420, 420, true);
|
|
38
|
+ $image->getImage()->setImageFormat(self::IMAGES_FORMAT);
|
|
39
|
+ try
|
|
40
|
+ {
|
|
41
|
+ $image->getImage()->writeImage($path);
|
|
42
|
+ return ImagesDataAccess::addId($dbo);
|
|
43
|
+ }
|
|
44
|
+ catch (Exception $e)
|
|
45
|
+ {
|
|
46
|
+ try
|
|
47
|
+ {
|
|
48
|
+ unlink($path);
|
|
49
|
+ }
|
|
50
|
+ catch (Exception $e)
|
|
51
|
+ {
|
|
52
|
+ }
|
|
53
|
+ abort(500, "Failed to save image");
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ return null;
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ public static function del($image_id, LuticateUsersDbo $_user)
|
17
|
60
|
{
|
|
61
|
+ $imageDbo = self::getById($image_id);
|
|
62
|
+ if ($imageDbo->getUserId() != $_user->getId()) {
|
|
63
|
+ self::unauthorized("You do not own this image");
|
|
64
|
+ }
|
18
|
65
|
return true;
|
19
|
66
|
}
|
20
|
67
|
}
|