/** * @return \Response */ public function show() { $result = []; $type = Input::get('type', null); $id = Input::get('id', null); $postId = Input::get('post', null); $albumId = Input::get('album', null); $attachment = Attachment::where(['id' => $id, 'type' => $type])->firstOrFail(); $result['attachment'] = ['type' => $attachment->type]; $result['before'] = []; $result['after'] = []; switch ($attachment->type) { case 'photo': $result['attachment']['src'] = $attachment->srcs['src_big']; break; case 'video': $result['attachment']['player'] = $attachment->srcs['player']; break; } if ($albumId) { $album = Attachment::find($albumId); $other_photos = $album->childs()->where('child_attachment_id', '<>', $attachment->id)->get(); $result['album'] = ['title' => $album->title]; foreach ($other_photos as $photo) { $result[$photo->id > $attachment->id ? 'after' : 'before'][] = ['type' => $photo->type, 'src' => $photo->srcs['src_big'], 'url' => route('attachment_show', ['id' => $photo->id, 'type' => 'photo', 'album' => $album->id, 'post' => $postId])]; } } return view('post.attachments.show', $result); }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // $attachment = Attachment::find($id); DB::beginTransaction(); // delete thumbnail if (\Input::get('name') == 'property') { $delete_thumb = \App\PropertyMeta::where('value', $attachment->file)->delete(); } if ($attachment->file) { if (\Input::get('name')) { \File::delete('uploads/images/' . \Input::get('name') . '/' . $attachment->file); } else { \File::delete('uploads/files/' . $attachment->file); } } $attachment->delete(); DB::commit(); return response()->json(array('status' => 200, 'monolog' => array('title' => 'delete success', 'message' => 'object has been deleted'), 'id' => $id)); }
/** * * @param string $action view|download|base64 * <p><b>view: </b>presenta el archivo en el navegador</p> * <p><b>download: </b>descarga el archivo</p> * <p><b>base64: </b>envia el archivo en base64</p> * @param int $id * @param string $key MD5($id . MD5('lorapp')). Evita que al cambiar $id se muestre otro archivo * @return file */ public static function getAttachment($action, $id, $key = null) { $data = ''; if ($key == MD5($id . MD5('lorapp'))) { $file = Attachment::find($id); $name = $file->name; $mime = $file->mime; $size = $file->size; $encode = $file->encode; $data = $file->file; if ($encode == 'base64') { $data = base64_decode($file->file); } $upload_path = $file->upload_path; if ($upload_path) { $upload_path = $file_path = public_path() . DIRECTORY_SEPARATOR . $file->upload_path . DIRECTORY_SEPARATOR . $file->name; $data = file_get_contents($upload_path); } if ($action == 'view') { return Response::make($data, 200, array('Content-type' => $mime, 'Content-length' => $size)); } else { if ($action == 'download') { return Response::make($data, 200, array('Content-type' => $mime, 'Content-length' => $size, 'Content-Disposition' => 'attachment; filename=' . $name)); } else { if ($action == 'base64') { return 'data:' . $file->mime . ';base64,' . $data; } } } } else { if ($action == 'name') { $file = Attachment::where('name', '=', $id)->first(); $name = $file->name; $mime = $file->mime; $size = $file->size; $encode = $file->encode; $data = $file->file; if ($encode == 'base64') { $data = base64_decode($file->file); } $data = $file->file; return Response::make($data, 200, array('Content-type' => $mime, 'Content-length' => $size)); } else { return Response::make($data, 200, array('Content-type' => '', 'Content-length' => 0)); } } }
public function update($id, Request $request) { $validator = Validator::make($request->all(), ['title' => 'required | min:3', 'headline' => 'sometimes|min:3', 'body' => 'sometimes | min:3', 'attachments' => 'required', 'published' => 'required|date', 'expires' => 'sometimes|after:' . $request->published]); if ($validator->passes()) { $post = Update::withTrashed()->find($id); $post->title = $request->title; $post->content = $request->body; $post->published_on = $request->published; $post->headline = $request->headline; if (isset($request->expires)) { $post->expires_on = $request->expires; } $post->category_id = $request->category; if ($post->save()) { if ($request->delete) { foreach ($request->delete as $deletion) { $attachment = Attachment::find($deletion); $attachment->delete(); } } $request->featured = $request->featured == '' ? null : $request->featured; $post->featured = $request->featured; $post->save(); if ($request->destroy) { $post->delete(); } else { if (!is_null($post->deleted_at)) { $post->restore(); } } $files = $request->file('attachments'); if (count($files)) { if (!is_dir($this->upload_dir . $post->id)) { mkdir($this->upload_dir . $post->id); } $success = true; foreach ($files as $file) { if (!is_null($file) && $file->isValid()) { if ($file->move($this->upload_dir . $post->id, $file->getClientOriginalName())) { $attachment = new Attachment(); $attachment->filename = $file->getClientOriginalName(); $attachment->size = $file->getClientSize(); $attachment->update_id = $post->id; $attachment->type = pathinfo($this->upload_dir . $post->id . '/' . $file->getClientOriginalName(), PATHINFO_EXTENSION); $attachment->save(); } else { $success = false; } } } if ($success) { return redirect('about/galleries/' . $post->id . '/edit'); } } return redirect('about/galleries'); } else { return 'Error'; } } else { return redirect('about/galleries/' . $post->id . '/edit')->withInput(); } }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, Request $request) { $post = Update::withTrashed()->find($id); $post->title = $request->title; $post->content = $request->body; $post->published_on = $request->published; $post->headline = $request->headline; if (isset($request->expires)) { $post->expires_on = $request->expires; } $post->category_id = $request->category; if ($post->save()) { if ($request->delete) { foreach ($request->delete as $deletion) { $attachment = Attachment::find($deletion); $attachment->delete(); } } $request->featured = $request->featured == '' ? null : $request->featured; $post->featured = $request->featured; $post->save(); if ($request->destroy) { $post->delete(); } else { if (!is_null($post->deleted_at)) { $post->restore(); } } $files = $request->file('attachments'); if (count($files)) { if (!is_dir($this->upload_dir . $post->id)) { mkdir($this->upload_dir . $post->id); } $success = true; foreach ($files as $file) { if (!is_null($file) && $file->isValid()) { if ($file->move($this->upload_dir . $post->id, $file->getClientOriginalName())) { $attachment = new Attachment(); $attachment->filename = $file->getClientOriginalName(); $attachment->size = $file->getClientSize(); $attachment->update_id = $post->id; $attachment->type = pathinfo($this->upload_dir . $post->id . '/' . $file->getClientOriginalName(), PATHINFO_EXTENSION); $attachment->save(); } else { $success = false; } } } if ($success) { return redirect('academylife/sports-news/' . $post->id . '/edit'); } } return redirect('academylife/sports-news'); } else { return 'Error'; } }