Browse Source

begin image del

develop
Robin Thoni 8 years ago
parent
commit
c43ed3dfd7

+ 48
- 1
app/Http/Business/ImagesBusiness.php View File

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

+ 9
- 2
app/Http/Controller/ImagesController.php View File

@@ -3,6 +3,8 @@
3 3
 namespace App\Http\Controller;
4 4
 
5 5
 use App\Http\DBO\ImageUploadDbo;
6
+use Luticate\Auth\DBO\LuticateUsersDbo;
7
+use Luticate\Utils\Dbo\LuIntDbo;
6 8
 use Luticate\Utils\LuController;
7 9
 use App\Http\Business\ImagesBusiness;
8 10
 use App\Http\DBO\ImagesDbo;
@@ -13,8 +15,13 @@ class ImagesController extends LuController {
13 15
         return new ImagesBusiness();
14 16
     }
15 17
 
16
-    public function upload(ImageUploadDbo $image)
18
+    public function upload(ImageUploadDbo $image, LuticateUsersDbo $_user)
17 19
     {
18
-        return ImagesBusiness::upload($image);
20
+        return ImagesBusiness::upload($image, $_user);
21
+    }
22
+    
23
+    public function del(LuIntDbo $image_id, LuticateUsersDbo $_user)
24
+    {
25
+        return ImagesBusiness::del($image_id->getInt(), $_user);
19 26
     }
20 27
 }

+ 4
- 0
app/Http/routes.php View File

@@ -13,8 +13,12 @@ LuticateBusiness::setupAuth();
13 13
 LuticateBusiness::setupRoutes("/api/luticate");
14 14
 LuDocBusiness::setupRoutes("/api/luticate");
15 15
 
16
+$image_id = "{image_id:$int}";
17
+
16 18
 $route->post("/api/effects/{effect}/apply", "Effects", "apply", Permissions::UPLOAD);
17 19
 
18 20
 $route->post("/api/images/upload", "Images", "upload", Permissions::UPLOAD);
19 21
 
22
+$route->post("/api/images/$image_id/del", "Images", "del", Permissions::UPLOAD);
23
+
20 24
 //sleep(2);

Loading…
Cancel
Save