From 07a13fcfed76d3336224da9728709d38e04d4280 Mon Sep 17 00:00:00 2001
From: Daniel Supernault <danielsupernault@gmail.com>
Date: Wed, 22 Jan 2020 16:01:01 -0700
Subject: [PATCH] Update StoryController, orientate story media and strip exif

---
 app/Http/Controllers/StoryController.php | 44 +++++++++++++++---------
 1 file changed, 27 insertions(+), 17 deletions(-)

diff --git a/app/Http/Controllers/StoryController.php b/app/Http/Controllers/StoryController.php
index c3443dd68..caec942d0 100644
--- a/app/Http/Controllers/StoryController.php
+++ b/app/Http/Controllers/StoryController.php
@@ -10,6 +10,7 @@ use App\Story;
 use App\StoryView;
 use App\Services\StoryService;
 use Cache, Storage;
+use Image as Intervention;
 use App\Services\FollowerService;
 
 
@@ -41,23 +42,8 @@ class StoryController extends Controller
 			abort(400, 'You have reached your limit for new Stories today.');
 		}
 
-		$monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12);
-		$sid = Str::uuid();
-		$rid = Str::random(6).'.'.Str::random(9);
-
 		$photo = $request->file('file');
-
-		$mimes = explode(',', config('pixelfed.media_types'));
-		if(in_array($photo->getMimeType(), [
-			'image/jpeg',
-			'image/png'
-		]) == false) {
-			abort(400, 'Invalid media type');
-			return;
-		}
-
-		$storagePath = "public/_esm.t1/{$monthHash}/{$sid}/{$rid}";
-		$path = $photo->store($storagePath);
+		$path = $this->storePhoto($photo);
 
 		$story = new Story();
 		$story->duration = 3;
@@ -77,6 +63,30 @@ class StoryController extends Controller
 		];
 	}
 
+	protected function storePhoto($photo)
+	{
+		$monthHash = substr(hash('sha1', date('Y').date('m')), 0, 12);
+		$sid = Str::uuid();
+		$rid = Str::random(6).'.'.Str::random(9);
+		$mimes = explode(',', config('pixelfed.media_types'));
+		if(in_array($photo->getMimeType(), [
+			'image/jpeg',
+			'image/png'
+		]) == false) {
+			abort(400, 'Invalid media type');
+			return;
+		}
+
+		$storagePath = "public/_esm.t1/{$monthHash}/{$sid}/{$rid}";
+		$path = $photo->store($storagePath);
+		$fpath = storage_path('app/' . $path);
+		$img = Intervention::make($fpath);
+		$img->orientate();
+		$img->save($fpath, config('pixelfed.image_quality'));
+		$img->destroy();
+		return $path;
+	}
+
 	public function apiV1Delete(Request $request, $id)
 	{
 		abort_if(!config('instance.stories.enabled') || !$request->user(), 404);
@@ -107,9 +117,9 @@ class StoryController extends Controller
 		$groupBy = config('database.default') == 'pgsql' ? 'id' : 'profile_id';
 
 		$stories = Story::with('profile')
+		->groupBy($groupBy)
 		->whereIn('profile_id', $following)
 		->where('expires_at', '>', now())
-		->groupBy($groupBy)
 		->orderByDesc('expires_at')
 		->take(9)
 		->get()