Beispiel #1
0
 public function testFind()
 {
     $kirby = $this->kirbyInstance();
     $site = $this->siteInstance($kirby);
     $page = new Page($site, 'tests/file-extension-case-test');
     $files = new Files($page);
     // test simple find by a single filename
     $result = $files->find('a.json');
     $this->assertInstanceOf('File', $result);
     // test simple find with multiple uris
     $result = $files->find('a.json', 'b.json');
     $this->assertInstanceOf('Files', $result);
     $this->assertEquals(2, $result->count());
     // test find by array
     $result = $files->find(['a.json', 'b.json']);
     $this->assertInstanceOf('Files', $result);
     $this->assertEquals(2, $result->count());
 }
 public function deleteFile($id)
 {
     $file = Files::find($id);
     if ($file->OwnerID == Auth::user()->StudentID) {
         Files::whereId($id)->update(array('delFlag' => 1));
         return Redirect::back()->with('message', 'File successfully deleted.');
     }
     return Redirect::back()->with('message', 'Error accessing file.');
 }
Beispiel #3
0
 public function put_index()
 {
     $data = file_get_contents('php://input');
     $temp = json_decode($data);
     $new = Files::find($temp->id);
     $new->text = $temp->text;
     $new->parent_id = $temp->parentId;
     $new->save();
     $array = array('success' => 'true');
     $json = json_encode($array);
     return $json;
 }
 function actionPutout()
 {
     $this->_pathway->addStep('媒资编目');
     $id = $this->_context->id;
     $file = Files::find()->getById($id);
     if ($file->isNewRecord()) {
         return '记录不存在或已删除';
     }
     if (!file_exists($file->path . $file->name . '.' . $file->ext)) {
         return '文件不存在或已删除';
     }
     //保存编目信息
     if ($this->_context->isPOST()) {
         $file->level = $this->_context->level;
         if (!isset($this->_context->groups) || in_array('all', $this->_context->groups)) {
             $file->groups = 'all';
         } else {
             $file->groups = ',';
             foreach ($this->_context->groups as $v) {
                 $file->groups .= $v . ',';
             }
         }
         $file->is_download = $this->_context->is_download;
         $file->status = $this->_context->status;
         $file->putout_username = $this->_view['currentUser']['username'];
         $file->putout_at = time();
         try {
             $file->save();
         } catch (QDB_ActiveRecord_ValidateFailedException $ex) {
             return '提交失败!';
         }
         //更新索引
         $filesCounter = FilesCounter::find()->getById(1);
         if ($filesCounter->isNewRecord()) {
             $filesCounter = new FilesCounter();
             $filesCounter->id = 1;
         }
         $filesCounter->file_id = $file->id();
         try {
             $filesCounter->save();
             @exec(Q::ini('appini/search/sphinxDelta'));
         } catch (QDB_ActiveRecord_ValidateFailedException $ex) {
             return '更新索引失败!';
         }
         return '提交成功!';
     }
     $this->_view['file'] = $file;
     $this->_view['levels'] = Levels::find('enabled=1')->getAll();
     $this->_view['groups'] = Groups::find('enabled=1')->getAll();
 }
Beispiel #5
0
 public function getDelete($idCourse, $idInscription, $id = '')
 {
     if ($id != '') {
         $file = Files::find($id);
         if (file_exists(str_replace('//', '/', public_path($file->url)))) {
             unlink(public_path($file->url));
             Files::destroy($file->id);
         } else {
             Files::destroy($file->id);
         }
         return Redirect::to(self::parseRoute($idCourse, $idInscription));
     } else {
         return Redirect::to(self::parseRoute($idCourse, $idInscription));
     }
 }
Beispiel #6
0
 public function testConstruction()
 {
     $kirby = $this->kirbyInstance();
     $site = $this->siteInstance($kirby);
     $page = new Page($site, '1-a');
     $files = new Files($page);
     $this->assertInstanceOf('Kirby', $files->kirby());
     $this->assertEquals($kirby, $files->kirby());
     $this->assertInstanceOf('Site', $files->site());
     $this->assertEquals($site, $files->site());
     $this->assertInstanceOf('Page', $files->page());
     $this->assertEquals($page, $files->page());
     $this->assertEquals(1, $files->count());
     $this->assertInstanceOf('File', $files->find('test.js'));
 }
 function actionCatalog()
 {
     $this->_pathway->addStep('媒资编目');
     $id = $this->_context->id;
     $file = Files::find()->getById($id);
     if ($file->isNewRecord()) {
         return '记录不存在或已删除';
     }
     if (!file_exists($file->path . $file->name . '.' . $file->ext)) {
         return '文件不存在或已删除';
     }
     //保存编目信息
     if ($this->_context->isPOST()) {
         //保存基本信息
         if (isset($_POST['title']) && $_POST['title'] != '') {
             $file->title = $_POST['title'];
         }
         if (isset($_POST['category_id']) && $_POST['category_id'] != '') {
             $category = Category::find()->getById($_POST['category_id']);
             $file->category_id = $category->id;
             $file->category_name = $category->name;
         }
         $file->catalogInfo = $this->_context->catalog;
         $file->status = 1;
         $file->catalog_username = $this->_view['currentUser']['username'];
         $file->catalog_at = time();
         try {
             $file->save();
         } catch (QDB_ActiveRecord_ValidateFailedException $ex) {
             return '编目失败!';
         }
         return '编目完成!';
     }
     $this->_view['file'] = $file;
     $this->_view['category'] = Category::getArrayTree();
 }
Beispiel #8
0
 static function getPreviewFileStream($id)
 {
     $file = Files::find()->getById($id);
     if ($file->isNewRecord()) {
         return;
     }
     $fileName = rtrim($file->path, '/\\') . DS . $file->name . '.' . $file->ext;
     if ($file->type == 1 || $file->type == 2) {
         $fileNamePreview = rtrim($file->path, '/\\') . DS . $file->name . '-preview.flv';
         $lock = $fileNamePreview . '.lock';
         while (true) {
             if (!file_exists($lock)) {
                 break;
             }
         }
         if (!file_exists($fileNamePreview) || !@filesize($fileNamePreview)) {
             $lock = $fileNamePreview . '.lock';
             if (!file_exists($lock)) {
                 @ignore_user_abort(1);
                 @set_time_limit(0);
                 @touch($lock);
                 $ffmpegPath = Q::ini('appini/catalog/ffmpegPath');
                 $ffmpegParameter = Q::ini('appini/catalog/ffmpegParameter');
                 //$command = "$ffmpegPath -i \"$fileName\" -y -ab 56 -ar 22050 -r 15 -b 500 -s 320x240 \"$fileNamePreview\"";
                 //$command = "$ffmpegPath -i \"$fileName\" -y -ab 56 -ar 22050 -r 15 -s 320x240 \"$fileNamePreview\"";
                 $command = "{$ffmpegPath} -i \"{$fileName}\" {$ffmpegParameter} \"{$fileNamePreview}\"";
                 //$command .= "&& $ffmpegPath -i \"$fileName\" -y -f image2 -ss 8 -t 0.011 -s 320x240 \"$fileNamePreview.jpg\"";
                 @exec($command);
                 @unlink($lock);
             }
         }
         $output = new QView_Output($file->title . '.' . $file->ext, 'video/x-flv');
         $output->addFile($fileNamePreview);
         return $output;
     } else {
         if ($file->type == 4) {
             if ($file->ext == 'txt') {
                 $handle = @fopen($fileName, 'r');
                 $data = @fread($handle, @filesize($fileName));
                 @fclose($handle);
                 return Helper_Util::encoding($data, 'utf-8');
             } else {
                 return '无法预览非文本文件(*.txt)格式';
             }
         } else {
             $output = new QView_Output($file->title);
             $output->addFile($fileName);
             if ($file->ext == 'jpg' || $file->ext == 'jpeg') {
                 $output->setMimeType('image/jpeg');
             }
             return $output;
         }
     }
 }
 /**
  * Export files
  *
  * @param void
  * @return null
  */
 function export()
 {
     $object_visibility = array_var($_GET, 'visibility', VISIBILITY_NORMAL);
     $exportable_modules = explode(',', array_var($_GET, 'modules', null));
     if (!is_foreachable($exportable_modules)) {
         $exportable_modules = null;
     }
     // if
     require_once PROJECT_EXPORTER_MODULE_PATH . '/models/ProjectExporterOutputBuilder.class.php';
     $output_builder = new ProjectExporterOutputBuilder($this->active_project, $this->smarty, $this->active_module, $exportable_modules);
     if (!$output_builder->createOutputFolder()) {
         $this->serveData($output_builder->execution_log, 'execution_log', null, FORMAT_JSON);
     }
     // if
     $output_builder->createAttachmentsFolder();
     $module_categories = Categories::findByModuleSection($this->active_project, $this->active_module, $this->active_module);
     $module_objects = Files::findByProject($this->active_project, STATE_VISIBLE, $object_visibility);
     $output_builder->setFileTemplate($this->active_module, $this->controller_name, 'index');
     $output_builder->smarty->assign('categories', $module_categories);
     $output_builder->smarty->assign('objects', $module_objects);
     $output_builder->outputToFile('index');
     // export files by categories
     if (is_foreachable($module_categories)) {
         foreach ($module_categories as $module_category) {
             if (instance_of($module_category, 'Category')) {
                 $objects = Files::find(array('conditions' => array('parent_id = ? AND project_id = ? AND type = ? AND state >= ? AND visibility >= ?', $module_category->getId(), $this->active_project->getId(), 'File', STATE_VISIBLE, $object_visibility), 'order' => 'boolean_field_1, datetime_field_1 DESC'));
                 $output_builder->smarty->assign(array('current_category' => $module_category, 'objects' => $objects));
                 $output_builder->outputToFile('category_' . $module_category->getId());
             }
             // if
         }
         // foreach
     }
     // if
     // export files
     if (is_foreachable($module_objects)) {
         $output_builder->setFileTemplate($this->active_module, $this->controller_name, 'object');
         foreach ($module_objects as $module_object) {
             if (instance_of($module_object, 'File')) {
                 $revisions = $module_object->getRevisions();
                 $output_builder->outputAttachments($revisions);
                 $comments = $module_object->getComments($object_visibility);
                 $output_builder->outputObjectsAttachments($comments);
                 $output_builder->smarty->assign('object', $module_object);
                 $output_builder->smarty->assign('comments', $comments);
                 $output_builder->outputToFile('file_' . $module_object->getId());
             }
             // if
         }
         // foreach
     }
     // if
     $this->serveData($output_builder->execution_log, 'execution_log', null, FORMAT_JSON);
 }
 public function editname()
 {
     $id = Input::get('id');
     $name = Input::get('name');
     $file = Files::find($id);
     if ($file) {
         $file->name = $name;
         $file->save();
     } else {
         throw new Exception("File not found #{$id}");
     }
 }
Beispiel #11
0
 /**
  *	Delete File
  */
 public function getDeleteFile($id)
 {
     // Get the Id of the file
     $file = \Files::find($id);
     if ($file == null) {
         //File doesnt Exists
         return Response::json('error', 400);
     } else {
         if (file_exists($file['file_sys_ref'] . $file['file_md5'])) {
             //Delete the file from the system
             Log::info('File ' . $file['file_name'] . ' deleted by ' . Sentry::getUser()->id);
             \File::delete($file['file_sys_ref'] . $file['file_md5']);
             $file->forceDelete();
             return Response::json('success', 200);
         } else {
             Log::error(Sentry::getUser()->id . ' tried to delete the file ' . $file['file_name'] . ' but it doesnt exists on the disk !');
             $file->forceDelete();
             throw new \FileNotFoundException();
         }
     }
 }
Beispiel #12
0
 /**
  * Delete Entity
  * @param int, string
  * @return boolean
  */
 public static function deleteEntity($id, $type)
 {
     try {
         $dir;
         $entity;
         if ($type == 'task') {
             //Find Task
             $entity = \Task::withTrashed()->find($id);
             //Get the path of folder for uploads
             $dir = public_path() . '/uploads/tasks/' . $entity->folder;
         } elseif ($type == 'project') {
             //Get Project
             $entity = \Project::withTrashed()->find($id);
             //Get the  path of folder for uploads
             $dir = public_path() . '/uploads/projects/' . $entity->folder;
         }
         if (is_dir($dir)) {
             //If Uploads directory exists, get list of files
             $files = array_diff(scandir($dir), array('.', '..'));
             //Delete Each file
             foreach ($files as $file) {
                 unlink($dir . '/' . $file);
             }
             //Delete folder
             $deleteFolder = \File::deleteDirectory($dir);
             //Get the file attachment Ids
             $fileIds = \Fileref::where('parent_id', $id)->where('parent_type', $type)->lists('attachment_id');
             if (sizeof($fileIds) != 0) {
                 //Delete each file attachment entry from database
                 foreach ($fileIds as $fieldId) {
                     $deletefile = \Files::find($fieldId);
                     $deletefile->forceDelete();
                 }
             }
             //Delete the entity
             $entity->forceDelete();
         } else {
             //Folder doesn't exists, delete the entity
             $entity->forceDelete();
         }
         return true;
     } catch (\Exception $e) {
         \Log::error('Something Went Wrong in Admin Data Controller - deleteEntity():' . $e->getMessage());
         throw new \SomethingWentWrongException();
     }
 }
 function actionView()
 {
     $this->_pathway->addStep('资料详情');
     $id = $this->_context->id;
     $level = $this->_view['currentUser']['level_id'];
     $group_id = $this->_view['currentUser']['group_id'];
     $file = Files::find('id=? and level<=? and (groups like "%,?,%" or groups="all")', $id, $level, $group_id)->getOne();
     if ($file->isNewRecord()) {
         return '您没有阅读权限';
     }
     $this->_view['file'] = $file;
 }
 /**
  * 删除 资源分类
  */
 function actionCategoryDel()
 {
     $this->_pathway->addStep('删除资源库分类');
     $id = $this->_context->id;
     if ($id == 1) {
         return '不能删除';
     }
     $categorys = Category::find("id = {$id} or path like '%,{$id},%'")->setColumns('id')->getAll();
     $ids = array();
     //'';
     foreach ($categorys as $key => $category) {
         $ids[] = $category->id;
         //.',';
     }
     //if ($ids != '') $ids = substr($ids, 0, strlen($ids)-1);
     $count = Files::find("category_id in (?)", $ids)->getCount('id');
     if ($count > 0) {
         return '该分类不为空,无法删除!';
     }
     $categorys->destroy();
     return 'true';
 }
 /**
  * Return files by category
  *
  * @param Category $category
  * @param integer $min_state
  * @param integer $min_visibility
  * @return array
  */
 function findByCategory($category, $min_state = STATE_VISIBLE, $min_visibility = VISIBILITY_NORMAL)
 {
     return Files::find(array('conditions' => array('parent_id = ? AND type = ? AND state >= ? AND visibility >= ?', $category->getId(), 'File', $min_state, $min_visibility), 'order' => 'created_on DESC'));
 }
 function actionIndex()
 {
     $this->_pathway->addStep('素材上载');
     if ($this->_context->isPOST()) {
         if (isset($_POST["PHPSESSID"])) {
             session_id($_POST["PHPSESSID"]);
         }
         if (!isset($_FILES["filedata"]) || !is_uploaded_file($_FILES["filedata"]["tmp_name"]) || $_FILES["filedata"]["error"] != 0) {
             return '上传失败!';
         }
         $filePath = rtrim(Q::ini('appini/upload/filePath'), '/\\') . DS;
         Helper_Filesys::mkdirs($filePath);
         //获得上传文件夹
         $dir = 'data1';
         $i = 0;
         $handle = opendir($filePath);
         while ($name = readdir($handle)) {
             if ($name != "." && $name != "..") {
                 if (is_dir($filePath . $name) && substr($name, 0, 4) == 'data') {
                     $i++;
                     $dir = $name;
                 }
             }
         }
         closedir($handle);
         if ($i == 0) {
             Helper_Filesys::mkdirs($filePath . $dir);
         }
         //判断文件中的文件是否超出限制
         $j = 0;
         $handle = opendir($filePath . $dir);
         while ($name = readdir($handle)) {
             if ($name != "." && $name != "..") {
                 $j++;
             }
         }
         closedir($handle);
         if ($j > 65535) {
             $dir = 'data' . ($i + 1);
         }
         //得到编码后的文件夹及文件名
         $fileNameMd5 = md5($_FILES["filedata"]["name"] . '-' . microtime(true));
         $filePath .= $dir . DS . $fileNameMd5 . DS;
         //保存路径名
         $fileName = md5_file($_FILES["filedata"]["tmp_name"]);
         //文件名
         $fileExt = pathinfo($_FILES["filedata"]["name"], PATHINFO_EXTENSION);
         //扩展名
         //保存到数据库
         $file = new Files();
         $file->category_id = $this->_context->category_id;
         $file->category_name = $this->_context->category_name;
         $file->title = substr($_FILES["filedata"]["name"], 0, strrpos($_FILES["filedata"]["name"], '.'));
         $file->name = $fileName;
         $file->ext = $fileExt;
         $file->size = $_FILES["filedata"]["size"];
         $file->path = $filePath;
         $file->status = 0;
         $file->catalog_info = '';
         $file->upload_username = $this->_view['currentUser']['username'];
         $file->upload_at = time();
         try {
             $file->save();
         } catch (QDB_ActiveRecord_ValidateFailedException $ex) {
             if (isset($ex->validate_errors['name'])) {
                 return $ex->validate_errors['name'];
             } else {
                 if (isset($ex->validate_errors['type'])) {
                     return $ex->validate_errors['type'];
                 } else {
                     return '上传失败!' . $ex;
                 }
             }
         }
         //保存上传文件
         Helper_Filesys::mkdirs($filePath);
         if (!move_uploaded_file($_FILES["filedata"]["tmp_name"], $filePath . $fileName . '.' . $fileExt)) {
             $file->destroy();
             //保存文件失败回滚数据
             return '上传失败!';
         }
         //返回成功结果
         return 'true_' . url('admin::filecatalog/preview', array('id' => $file->id()));
     } else {
         $categoryId = $this->_context->category_id;
         $categoryId = isset($categoryId) ? $categoryId : 1;
         $category = Category::find()->getById($categoryId);
         $this->_view['category'] = $category;
         $categoryIds = Category::getChildrenIds($categoryId);
         if (count($categoryIds)) {
             //获得历史上传
             $files = Files::find('category_id in (?) and upload_username=?', $categoryIds, $this->_view['currentUser']['username'])->order('upload_at desc')->top(13)->getAll();
             $this->_view['files'] = $files;
         }
     }
 }
Beispiel #17
0
 public static function drop($id)
 {
     $file = Files::find($id);
     if ($file) {
         Files::dropFile($file->path, $id);
         Log::warning("Drop file #{$id} - {$file->path}");
     }
     return Files::destroy($id);
 }