/** * @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); }
function delete_attachments($article_id) { $attachments = Attachment::where('article_id', 'LIKE', $article_id)->get(); foreach ($attachments as $attachment) { if (File::exists($attachment->path)) { File::delete($attachment->path); } $attachment->delete(); } }
/** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index($name) { $page = Page::where('name', 'LIKE', $name)->first(); $section = Section::where('id', 'LIKE', $page->section_id)->first(); $articles = Article::where('page_id', 'LIKE', $page->id)->orderBy('publish_time', 'desc')->get(); $articles_attachments = []; foreach ($articles as $article) { $articles_attachments[$article->id] = Attachment::where('article_id', 'LIKE', $article->id)->get(); } return view('page.index', ['page' => $page, 'section' => $section, 'articles' => $articles, 'articles_attachments' => $articles_attachments]); }
/** * Display the specified resource. * * @param int $id * @return Response */ public function show(Request $request) { $filename = $request->route('file'); $post = $request->route('post'); $path = '../storage/app/images/uploads/'; $filepath = $path . $post . '/' . $filename; $find = Attachment::where('update_id', $post)->where('filename', $filename); if ($find->count()) { $file_ext = $find->first()->type; if (file_exists($filepath)) { $file_size = filesize($filepath); $file = @fopen($filepath, "rb"); if ($file) { // set the headers, prevent caching header("Pragma: public"); header("Expires: -1"); header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0"); header("Content-Disposition: attachment; filename=\"{$filename}\""); // set the mime type based on extension, add yours if needed. $ctype = "application/octet-stream"; header("Content-Type: " . $ctype); header("Content-Length: {$file_size}"); set_time_limit(0); while (!feof($file)) { print @fread($file, 1024 * 8); ob_flush(); flush(); if (connection_status() != 0) { @fclose($file); exit; } } // file save was a success @fclose($file); exit; } else { // file couldn't be opened header("HTTP/1.0 500 Internal Server Error"); exit; } } } }
/** * @param array $data * @return Attachment|\Illuminate\Database\Eloquent\Model|null|static */ protected static function __getAttachment(array $data = []) { $atatch = null; $whereParam = ['external_id' => $data['id'], 'type' => $data['type']]; switch ($data['type']) { case 'link': $url = $data['srcs']['url']; $url = str_replace('/', '\\\\/', $url); $attach = Attachment::where('srcs', 'like', '%' . $url . '%')->where('type', $data['type'])->first(); if (is_null($attach)) { $attach = new Attachment($whereParam); } break; default: $attach = Attachment::firstOrNew($whereParam); break; } return $attach; }
/** * * @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)); } } }