/** * Delete an entire folder * * @return boolean */ public function deleteFolder() { try { DB::beginTransaction(); // Get all the files in this folder // and physical deleting files $files = $this->files; if (!empty($files)) { foreach ($files as $file) { Utilities::deleteFile($file->name); } // Delete all record of files in this folder FmFile::where('folder_id', $this->id)->delete(); } // Delete this folder $this->delete(); DB::commit(); return true; } catch (Exception $e) { DB::rollback(); return false; } }
/** * Download file * * @param int $id * * @return Response */ public function downloadFile($id) { $file = FmFile::where('status', Constants::$COMMON_STATUSES['public'])->find($id); if (empty($file)) { App::abort('404'); } $filePath = Utilities::getFileUploadPath() . $file->name; $content = file_get_contents($filePath); $contentLength = fileSize($filePath); $headers = array('Content-Length' => $contentLength, 'Content-disposition' => 'attachment; filename=' . $file->original_name); $finfo = new \finfo(FILEINFO_MIME_TYPE); $contentType = $finfo->buffer($content); if ($contentType == 'audio/mpeg') { $headers['Content-Type'] = 'application/octet-stream'; } $response = Response::make($content); foreach ($headers as $headerName => $headerValue) { $response->header($headerName, $headerValue); } return $response; }
/** * Process upload file to server and save to database * * @param int $folderId * @param array $errors */ private function processUploadFile($folderId, &$errors) { if (!Input::hasFile('uploadFile')) { $errors = trans('message.common.error.file_not_found'); return false; } $currentUser = $this->getCurrentUser(); // Find folder $folder = Folder::where('id', $folderId)->where('owner_id', $currentUser->id)->get()->first(); if (empty($folder)) { $errors = trans('message.folder.error.not_found'); return false; } $file = Input::file('uploadFile'); // Upload file to server $uploadedFileName = Utilities::uploadFile($file, false); if (empty($uploadedFileName)) { $errors = trans('message.common.error.upload_failed'); return false; } // Save data to database $fileSize = $file->getClientSize(); $model = new FmFile(); $model->name = $uploadedFileName; $model->original_name = $file->getClientOriginalName(); $model->file_size = $fileSize; $model->folder_id = $folderId; $model->status = Input::get('status'); $model->file_type = Input::get('file_type'); if (!$model->save()) { $errors = $model->errors(); return false; } return true; }