|
@@ -0,0 +1,68 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by IntelliJ IDEA.
|
|
|
+ * User: kelyliang
|
|
|
+ * Date: 2024/5/8
|
|
|
+ * Time: 下午 03:47
|
|
|
+ */
|
|
|
+
|
|
|
+namespace App\Services\File;
|
|
|
+
|
|
|
+
|
|
|
+use App\Http\Resources\API\FileDownloadResource;
|
|
|
+use App\Models\Folder;
|
|
|
+use Illuminate\Support\Collection;
|
|
|
+use App\Models\Enums\FileObjectType;
|
|
|
+use function Nette\Utils\isEmpty;
|
|
|
+
|
|
|
+class FolderUrlService
|
|
|
+{
|
|
|
+ public function folderFormat(Collection $folders){
|
|
|
+ $item=[];
|
|
|
+ foreach ($folders as $folder){
|
|
|
+ $folderIds=[];
|
|
|
+ $folderIds = [...$folderIds, ...explode(",", $folder->path)];
|
|
|
+ //排除自己的文件夹
|
|
|
+ $folderPathArray = array_unique(array_diff($folderIds, [$folder->id]));
|
|
|
+ $folders = Folder::query()->whereIn("id", $folderPathArray)->pluck("name", "id");
|
|
|
+ if($folders->isEmpty()){
|
|
|
+ $folder['folder_path']='/';
|
|
|
+ $item[]=$folder;
|
|
|
+ }else{
|
|
|
+ $folderPath = [];
|
|
|
+ foreach($folderPathArray as $folderId) {
|
|
|
+ $folderPath[] = $folders->get($folderId);
|
|
|
+ }
|
|
|
+ $folder['folder_path']=implode('/', $folderPath);
|
|
|
+ $item[]=$folder;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $item;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function filesFormat(Collection $files)
|
|
|
+ {
|
|
|
+ $folderIds = [];
|
|
|
+ foreach ($files as $file) {
|
|
|
+ $folderIds = [...$folderIds, ...explode(",", $file->folder?->path)];
|
|
|
+ }
|
|
|
+
|
|
|
+ $folders = Folder::query()->whereIn("id", array_unique(array_filter($folderIds)))->pluck("name", "id");
|
|
|
+
|
|
|
+ $items = [];
|
|
|
+ foreach ($files as $file) {
|
|
|
+
|
|
|
+ $fileDownloadResource = (new FileDownloadResource($file))->toArray(request());
|
|
|
+
|
|
|
+ $folderPath = [];
|
|
|
+ foreach(array_filter(explode(",", $file->folder?->path)) as $folderId) {
|
|
|
+ $folderPath[] = $folders->get($folderId);
|
|
|
+ }
|
|
|
+
|
|
|
+ $fileDownloadResource['folder_path'] = "/" . implode('/', $folderPath);
|
|
|
+
|
|
|
+ $items[] = $fileDownloadResource;
|
|
|
+ }
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+}
|