File.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\controller;
  3. ini_set('max_execution_time', 300);
  4. use app\BaseController;
  5. use app\model\FileModel;
  6. class File extends BaseController
  7. {
  8. private $files = [];
  9. public function list(): \think\response\Json
  10. {
  11. $this->getAdmin();
  12. $limit = $this->request->post('limit', 15);
  13. $name = $this->request->post('search.path', false);
  14. $user_id = $this->request->post("search.user_id",false);
  15. $sql = [];
  16. if ($name) {
  17. $sql[] = ['mime_type|path', 'like', '%' . $name . '%'];
  18. }
  19. if($user_id){
  20. $sql["user_id"] = $user_id;
  21. }
  22. $list = FileModel::with("user")->where($sql);
  23. $list = $list->order('id', 'desc')->paginate($limit);
  24. return $this->success('ok', $list);
  25. }
  26. function del(): \think\response\Json
  27. {
  28. is_demo_mode(true);
  29. $this->getAdmin();
  30. $ids = $this->request->post('ids', []);
  31. $res = FileModel::where('id', 'in', $ids)->select()->toArray();
  32. foreach ($res as $k => $v) {
  33. $p = joinPath(public_path(), $v['path']);
  34. if (file_exists($p)) {
  35. @unlink($p);
  36. }
  37. FileModel::where('id', $v['id'])->delete();
  38. }
  39. return $this->success('删除成功');
  40. }
  41. private function countFilesInDirectory($directory): int
  42. {
  43. $fileCount = 0;
  44. // 获取目录中的文件和子目录
  45. $files = scandir($directory);
  46. foreach ($files as $file) {
  47. // 排除"."和".."
  48. if ($file != '.' && $file != '..') {
  49. $filePath = $directory . '/' . $file;
  50. // 如果是目录,则递归调用函数
  51. if (is_dir($filePath)) {
  52. $fileCount += $this->countFilesInDirectory($filePath);
  53. } else {
  54. // 如果是文件,则增加文件数量
  55. $this->files[] = joinPath("/", $filePath);
  56. $fileCount++;
  57. }
  58. }
  59. }
  60. return $fileCount;
  61. }
  62. function scanLocal(): \think\response\Json
  63. {
  64. is_demo_mode(true);
  65. $this->getAdmin();
  66. if (!is_dir(public_path("images"))) {
  67. return $this->success('扫描完成');
  68. }
  69. $this->countFilesInDirectory("images");
  70. $list = FileModel::limit(5000)->select()->toArray();
  71. foreach ($list as $key => $v) {
  72. $index = array_search(joinPath('/', $v['path']), $this->files);
  73. if ($index >= 0) {
  74. unset($this->files[$index]);
  75. }
  76. }
  77. $all = [];
  78. if (count($this->files) > 0) {
  79. foreach ($this->files as $key => $v) {
  80. $p = joinPath(public_path(), $v);
  81. $info = [];
  82. $info['path'] = $v;
  83. $info['user_id'] = null;
  84. $info['create_time'] = date('Y-m-d H:i:s');
  85. $info['size'] = filesize($p) ?? 0;
  86. $info['mime_type'] = mime_content_type($p) ?? 'null';
  87. $all[] = $info;
  88. }
  89. $file = new FileModel();
  90. $file->saveAll($all);
  91. }
  92. return $this->success("扫描完成");
  93. }
  94. }