Exemplo n.º 1
0
 /**
  * Return a photo as a response.
  *
  * @param int    $photoId
  * @param string $size
  *
  * @return \Illuminate\Http\Response
  */
 public function getPhoto($photoId, $size = 'large')
 {
     $photo = Photo::findOrFail($photoId);
     switch ($size) {
         case 'large':
             return response()->download(storage_path() . '/app/' . $photo->pathLarge);
         case 'small':
             return response()->download(storage_path() . '/app/' . $photo->pathSmall);
     }
     abort(404);
 }
Exemplo n.º 2
0
 /**
  * Return the new item view.
  *
  * @param int $itemId
  *
  * @return \Illuminate\View\View
  */
 public function getNew()
 {
     $categories = Category::leaves()->orderBy('category_id')->get();
     if (!session()->has('photos')) {
         $session_photos = [];
     } else {
         foreach (session('photos') as $session_photo) {
             $session_photos[] = \Hamjoint\Mustard\Media\Photo::find($session_photo['photo_id']);
         }
     }
     return view('mustard::item.new', ['categories' => $categories, 'item' => new Item(), 'listing_durations' => ListingDuration::all(), 'item_conditions' => ItemCondition::all(), 'photos' => $session_photos]);
 }
Exemplo n.º 3
0
 /**
  * Delete a photo.
  *
  * @param \Illuminate\Http\Request $request
  *
  * @return \Illuminate\Http\Response
  */
 public function postDeletePhoto(Request $request)
 {
     if (session()->has('photos')) {
         $photos = session('photos');
         foreach ($photos as $index => $photo) {
             if (in_array($request->input('file'), $photo)) {
                 $photo = Photo::find($photo['photo_id']);
                 $photo->delete();
                 unset($photos[$index]);
                 session('photos', $photos);
                 return response('', 200);
             }
         }
     }
     return response('', 400);
 }
Exemplo n.º 4
0
 /**
  * Create a new item.
  *
  * @param \Illuminate\Http\Request $request
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function postNew(ItemNew $request)
 {
     $file = $request->file('doc');
     $item = new Item();
     $item->name = $request->input('name');
     $item->description = $request->input('description');
     $item->auction = $request->input('type') == 'auction';
     $item->quantity = $request->input('type') == 'auction' ? 1 : $request->input('quantity');
     $item->commission = 0;
     $item->startPrice = $request->input('start_price');
     $item->biddingPrice = $request->input('type') == 'auction' ? $request->input('start_price') : null;
     $item->fixedPrice = $request->input('fixed_price');
     $item->reservePrice = $request->input('reserve_price');
     $item->startDate = strtotime($request->input('start_date') . ' ' . $request->input('start_time'));
     $item->startDate = $item->startDate < time() ? time() : $item->startDate;
     $item->duration = ListingDuration::where('duration', $request->input('duration'))->value('duration');
     $item->endDate = $item->getEndDate();
     $item->collectionLocation = $request->input('collection_location');
     $item->paymentOther = (bool) $request->input('payment_other');
     $item->returnsPeriod = $request->input('returns_period');
     $item->created = time();
     $item->condition()->associate($request->input('condition'));
     $item->seller()->associate(Auth::user());
     $item->save();
     foreach ((array) $request->input('categories') as $category_id) {
         if ($category = Category::find($category_id)) {
             $item->categories()->save($category);
         }
     }
     if (mustard_loaded('media')) {
         $photos = [];
         if ($request->hasFile('photos')) {
             foreach ($request->file('photos') as $file) {
                 $photos[] = ['real_path' => $file->getRealPath(), 'filename' => $file->getClientOriginalName()];
             }
         }
         if ($request->session()->has('photos')) {
             foreach ($request->session()->pull('photos') as $file) {
                 $photos[] = $file;
             }
         }
         $primary_set = false;
         foreach ($photos as $file) {
             $pp = new \PhotoProcessor($file['real_path']);
             try {
                 $pp->validate();
             } catch (\PhotoFormatUnknownException $e) {
                 self::formFlash(array_keys(array_except($request->all(), ['photos'])));
                 return redirect()->back()->withErrors([$file['filename'] . ' is not an image format we could recognise.']);
             }
             $photo = new \Hamjoint\Mustard\Media\Photo();
             if (!$primary_set) {
                 $photo->primary = $primary_set = true;
             }
             $photo->processed = false;
             $photo->item()->associate($item);
             $photo->save();
             $pp->process($photo);
         }
     }
     foreach ((array) $request->input('delivery_options') as $delivery_option) {
         $validator = \Validator::make($delivery_option, ['name' => 'required|min:3', 'price' => 'required|monetary', 'arrival_time' => 'required|intrange']);
         // Skip if fails validation
         if ($validator->fails()) {
             continue;
         }
         $do = new DeliveryOption();
         $do->name = $delivery_option['name'];
         $do->price = $delivery_option['price'];
         if (preg_match('/(\\d+)-(\\d+)/', $delivery_option['arrival_time'], $matches)) {
             $do->minArrivalTime = $matches[1];
             $do->maxArrivalTime = $matches[2];
         } else {
             $do->minArrivalTime = $do->maxArrivalTime = $delivery_option['arrival_time'];
         }
         $item->deliveryOptions()->save($do);
     }
     $item->seller->sendEmail('You have listed an item', 'mustard::emails.item.listed', ['item_id' => $item->itemId, 'item_name' => $item->name]);
     return redirect($item->url);
 }
Exemplo n.º 5
0
 /**
  * Process a photo and create a record.
  *
  * @param string $file
  *
  * @return self
  */
 public static function upload($file)
 {
     $photo = self::create();
     $image_manager = new ImageManager(['driver' => 'imagick']);
     $disk = Storage::disk(config('mustard.storage.disk', 'local'));
     if (!file_exists($file)) {
         RuntimeException("File {$file} does not exist");
     }
     $dest_dir = config('mustard.storage.photo.dir', 'mustard/photos');
     $quality = config('mustard.storage.photo.quality', 90);
     $disk->makeDirectory($dest_dir);
     $photo_id = $photo->getKey();
     Queue::push(function ($job) use($file, $quality, $dest_dir, $photo_id) {
         if (!file_exists($file)) {
             RuntimeException("File {$file} no longer exists");
         }
         $image_manager = new ImageManager(['driver' => 'imagick']);
         $disk = Storage::disk(config('mustard.storage.disk', 'local'));
         if ($file != "{$dest_dir}/{$photo_id}.jpg") {
             $image = $image_manager->make($file);
             $disk->put("{$dest_dir}/{$photo_id}.jpg", (string) $image->encode('jpg', $quality));
             $image->destroy();
         }
         $image = $image_manager->make($file)->heighten(83);
         $disk->put("{$dest_dir}/{$photo_id}_s.jpg", (string) $image->encode('jpg', $quality));
         $image->destroy();
         $image = $image_manager->make($file)->widen(500);
         $disk->put("{$dest_dir}/{$photo_id}_l.jpg", (string) $image->encode('jpg', $quality));
         $image->destroy();
         $photo = Photo::find($photo_id);
         $photo->processed = true;
         $photo->save();
         $job->delete();
     });
     return $photo;
 }