diff --git a/.gitignore b/.gitignore index 07807a1..1a57f21 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ /exemple/AirMedia/app_token /exemple/config/app_token /exemple/download/app_token +/exemple/FileSystem/app_token /app_token diff --git a/README.md b/README.md index 6bd2276..ca7700d 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ Ce projet est basé sur **composer**. Pensez à installer les dependences :) Jusqu'a présent, les fonctionalités suivantes ont été implémentées : +- AirMedia +- FileSystem + - FileSystem (Core) - Downloads - Download - Configuration diff --git a/exemple/FileSystem/fsListing.php b/exemple/FileSystem/fsListing.php new file mode 100644 index 0000000..8356d97 --- /dev/null +++ b/exemple/FileSystem/fsListing.php @@ -0,0 +1,24 @@ +authorize(); +$App->openSession(); + +/// Scan files in root +$FSListService = new \alphayax\freebox\api\v3\services\FileSystem\FileSystemListing( $App); +$FilesInRoot = $FSListService->getFilesFromDirectory( '/'); +foreach( $FilesInRoot as $fileInfo){ + echo '- '. $fileInfo->getPath() . PHP_EOL; +} + +/// Get a file information (Will throw an exception if file does not exists) +try { + $VideoFile = $FSListService->getFileInformation( '/Disque dur/Téléchargements/The.100.S03E14.FASTSUB.VOSTFR.1080p.HDTV.x264.AAC-GOBO2S/The.100.S03E14.FASTSUB.VOSTFR.1080p.HDTV.x264.AAC-GOBO2S.mkv'); + var_dump( $VideoFile); +} catch (Exception $e){ + echo $e->getMessage(); +} diff --git a/exemple/FileSystem/fsOperation.php b/exemple/FileSystem/fsOperation.php new file mode 100644 index 0000000..4ffcf24 --- /dev/null +++ b/exemple/FileSystem/fsOperation.php @@ -0,0 +1,54 @@ +authorize(); +$App->openSession(); + +$FSListService = new \alphayax\freebox\api\v3\services\FileSystem\FileSystemOperation( $App); + +/// Uncomment following lines and give existing file names to test +/* +$a = $FSListService->move( ['/Disque dur/Photos/vivi.jpg'], '/Disque dur/Videos/'); +var_dump( $a); + +$a = $FSListService->copy( ['/Disque dur/Photos/vivi.jpg'], '/Disque dur/Photos/vivi3.jpg'); +var_dump( $a); + +$b = $FSListService->remove( ['/Disque dur/Photos/vivi2.jpg']); +var_dump( $b); + +$c = $FSListService->cat( ['/Disque dur/Photos/vivi.jpg','/Disque dur/Photos/vivi3.jpg'], '/Disque dur/Photos/vivivivi.jpg'); +var_dump( $c); + +$d = $FSListService->archive(['/Disque dur/Photos/vivi.jpg','/Disque dur/Photos/vivi3.jpg'], '/Disque dur/Photos/vivi.zip'); +var_dump( $d); + +$e = $FSListService->extract( '/Disque dur/Photos/vivi.zip', '/Disque dur/Vidéos/', '', true); +var_dump( $e); + +$f = $FSListService->computeHash( '/Disque dur/Photos/vivi.jpg'); +var_dump( $f); +sleep( 2); +$g = $FSListService->getHashValue( $f->getId()); +var_dump( $g); + +$h = $FSListService->createDirectory( '/Disque dur/Photos/', 'toto'); +var_dump( $h); + +$i = $FSListService->rename('/Disque dur/Photos/toto', 'tutu'); +var_dump( $i); + +$j = $FSListService->download( '/Disque dur/Photos/vivi.jpg'); +var_dump( $j); +*/ + +/* +sleep(1); +$Task = new \alphayax\freebox\api\v3\services\FileSystem\FileSystemTask($App); +$a = $Task->getTaskById($e->getId()); +var_dump( $a); +*/ diff --git a/freebox/api/v3/Service.php b/freebox/api/v3/Service.php index 31fbac7..1dcd2fb 100644 --- a/freebox/api/v3/Service.php +++ b/freebox/api/v3/Service.php @@ -30,11 +30,13 @@ abstract class Service { } /** - * @param $service + * @param string $service + * @param bool $isJson + * @param bool $returnAsArray * @return \alphayax\freebox\utils\RestAuth */ - protected function getAuthService( $service){ - $rest = new \alphayax\freebox\utils\RestAuth( static::API_HOST . $service); + protected function getAuthService( $service, $isJson = true, $returnAsArray = true){ + $rest = new \alphayax\freebox\utils\RestAuth( static::API_HOST . $service, $isJson, $returnAsArray); $rest->setSessionToken( $this->application->getSessionToken()); return $rest; } diff --git a/freebox/api/v3/models/FileSystem/FileInfo.php b/freebox/api/v3/models/FileSystem/FileInfo.php new file mode 100644 index 0000000..574bee3 --- /dev/null +++ b/freebox/api/v3/models/FileSystem/FileInfo.php @@ -0,0 +1,145 @@ +path); + } + + /** + * @return string + */ + public function getName(){ + return $this->name; + } + + /** + * @return string + */ + public function getMimetype(){ + return $this->mimetype; + } + + /** + * @return string + * @see alphayax\freebox\api\v3\symbols\FileSystem\FileInfoType + */ + public function getType(){ + return $this->type; + } + + /** + * @return int + */ + public function getSize(){ + return $this->size; + } + + /** + * @return int + */ + public function getModification(){ + return $this->modification; + } + + /** + * @return int + */ + public function getIndex(){ + return $this->index; + } + + /** + * @return boolean + */ + public function isLink(){ + return $this->link; + } + + /** + * @return string + */ + public function getTarget(){ + return base64_decode( $this->target); + } + + /** + * @return boolean + */ + public function isHidden(){ + return $this->hidden; + } + + /** + * @return int + */ + public function getFoldercount(){ + return $this->foldercount; + } + + /** + * @return int + */ + public function getFilecount(){ + return $this->filecount; + } + + +} diff --git a/freebox/api/v3/models/FileSystem/FsTask.php b/freebox/api/v3/models/FileSystem/FsTask.php new file mode 100644 index 0000000..98db667 --- /dev/null +++ b/freebox/api/v3/models/FileSystem/FsTask.php @@ -0,0 +1,247 @@ +id; + } + + + /** + * @return string + * @see alphayax\freebox\api\v3\symbols\FileSystem\TaskType + */ + public function getType(){ + return $this->type; + } + + /** + * @return string + * @see alphayax\freebox\api\v3\symbols\FileSystem\TaskState + */ + public function getState(){ + return $this->state; + } + + /** + * @param string $state + * @see alphayax\freebox\api\v3\symbols\FileSystem\TaskState + */ + public function setState( $state){ + $this->state = $state; + } + + /** + * @return string + */ + public function getError(){ + return $this->error; + } + + /** + * @return int + */ + public function getCreatedTs(){ + return $this->created_ts; + } + + /** + * @return int + */ + public function getStartedTs(){ + return $this->started_ts; + } + + /** + * @return int + */ + public function getDoneTs(){ + return $this->done_ts; + } + + /** + * @return int + */ + public function getDuration(){ + return $this->duration; + } + + /** + * @return int + */ + public function getProgress(){ + return $this->progress; + } + + /** + * @return int + */ + public function getEta(){ + return $this->eta; + } + + /** + * @return string + */ + public function getFrom(){ + return $this->from; + } + + /** + * @return string + */ + public function getTo(){ + return $this->to; + } + + /** + * @return int + */ + public function getNfiles(){ + return $this->nfiles; + } + + /** + * @return int + */ + public function getNfilesDone(){ + return $this->nfiles_done; + } + + /** + * @return int + */ + public function getTotalBytes(){ + return $this->total_bytes; + } + + /** + * @return int + */ + public function getTotalBytesDone(){ + return $this->total_bytes_done; + } + + /** + * @return int + */ + public function getCurrBytes(){ + return $this->curr_bytes; + } + + /** + * @return int + */ + public function getCurrBytesDone(){ + return $this->curr_bytes_done; + } + + /** + * @return int + */ + public function getRate(){ + return $this->rate; + } + +} diff --git a/freebox/api/v3/services/FileSystem/FileSystemListing.php b/freebox/api/v3/services/FileSystem/FileSystemListing.php new file mode 100644 index 0000000..a0ae992 --- /dev/null +++ b/freebox/api/v3/services/FileSystem/FileSystemListing.php @@ -0,0 +1,53 @@ +getAuthService( self::API_FS_LS . $Directory_b64); + $rest->GET([ + 'onlyFolder' => $onlyFolder, + 'countSubFolder' => $countSubFolder, + 'removeHidden' => $removeHidden, + ]); + + $FsTask_xs = $rest->getCurlResponse()['result']; + $FsTasks = []; + foreach( $FsTask_xs as $fsTask_x) { + $FsTasks[] = new FileInfo( $fsTask_x); + } + return $FsTasks; + } + + /** + * Get file information + * @param string $DirectoryName + * @return FileInfo + */ + public function getFileInformation( $DirectoryName){ + $Directory_b64 = base64_encode( $DirectoryName); + $rest = $this->getAuthService( self::API_FS_INFO . $Directory_b64); + $rest->GET(); + + return new FileInfo( $rest->getCurlResponse()['result']); + } + +} diff --git a/freebox/api/v3/services/FileSystem/FileSystemOperation.php b/freebox/api/v3/services/FileSystem/FileSystemOperation.php new file mode 100644 index 0000000..c374882 --- /dev/null +++ b/freebox/api/v3/services/FileSystem/FileSystemOperation.php @@ -0,0 +1,278 @@ +getAuthService( self::API_FS_MV); + $rest->POST([ + 'files' => $source_b64, + 'dst' => $destination_b64, + 'mode' => $conflictMode, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Copy files + * @param string[] $sourceFiles + * @param string $destination + * @param string $conflictMode + * @return FsTask + */ + public function copy( array $sourceFiles = [], $destination, $conflictMode = 'recent'){ + /// Convert all paths in base64 + $destination_b64 = base64_encode( $destination); + $source_b64 = []; + foreach( $sourceFiles as $sourceFile){ + $source_b64[] = base64_encode( $sourceFile); + } + + $rest = $this->getAuthService( self::API_FS_CP); + $rest->POST([ + 'files' => $source_b64, + 'dst' => $destination_b64, + 'mode' => $conflictMode, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Delete files + * @param string[] $RemoveFiles + * @return FsTask + */ + public function remove( array $RemoveFiles = []){ + /// Convert all paths in base64 + $removedFiles_b64 = []; + foreach( $RemoveFiles as $sourceFile){ + $removedFiles_b64[] = base64_encode( $sourceFile); + } + + $rest = $this->getAuthService( self::API_FS_RM); + $rest->POST([ + 'files' => $removedFiles_b64, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Concatenate files (Miaw ^^) + * @param string[] $fileParts : The list of files to concatenate + * @param string $destination : The destination file + * @param bool $isMultiVolumes : Enable multi-volumes mode, it will start at XXX001 and concatenate XXX002, XXX003, ... + * @param bool $isToDelete : Deletes source files + * @param bool $isToOverwrite : Overwrites the destination + * @param bool $isToAppend : Append to the destination + * @return FsTask + */ + public function cat( array $fileParts = [], $destination, $isMultiVolumes = false, $isToDelete = false, $isToOverwrite = false, $isToAppend = false){ + /// Convert all paths in base64 + $destination_b64 = base64_encode( $destination); + $fileParts_b64 = []; + foreach($fileParts as $FilePart){ + $fileParts_b64[] = base64_encode( $FilePart); + } + + $rest = $this->getAuthService( self::API_FS_CAT); + $rest->POST([ + 'files' => $fileParts_b64, + 'dst' => $destination_b64, + 'multi_volumes' => $isMultiVolumes, + 'delete_files' => $isToDelete, + 'append' => $isToAppend, + 'overwrite' => $isToOverwrite, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Create an archive + * @param string[] $fileParts : The list of files to concatenate + * @param string $destination : The destination file + * @return FsTask + */ + public function archive( array $fileParts = [], $destination){ + /// Convert all paths in base64 + $destination_b64 = base64_encode( $destination); + $fileParts_b64 = []; + foreach($fileParts as $FilePart){ + $fileParts_b64[] = base64_encode( $FilePart); + } + + $rest = $this->getAuthService( self::API_FS_ARCHIVE); + $rest->POST([ + 'files' => $fileParts_b64, + 'dst' => $destination_b64, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Extract an archive + * @param string $source : The archive file + * @param string $destination : The destination folder + * @param string $password : The archive password + * @param bool $isToDelete : Delete archive after extraction + * @param bool $isToOverwrite : Overwrites the destination + * @return FsTask + */ + public function extract( $source, $destination, $password = '', $isToDelete = false, $isToOverwrite = false){ + /// Convert all paths in base64 + $source_b64 = base64_encode( $source); + $destination_b64 = base64_encode( $destination); + + $rest = $this->getAuthService( self::API_FS_EXTRACT); + $rest->POST([ + 'src' => $source_b64, + 'dst' => $destination_b64, + 'password' => $password, + 'delete_archive' => $isToDelete, + 'overwrite' => $isToOverwrite, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Repair files from a .par2 + * @param string $source : The .par2 file + * @param bool $isToDelete : Delete par2 files after repair + * @return FsTask + */ + public function repair( $source, $isToDelete = false){ + /// Convert all paths in base64 + $source_b64 = base64_encode( $source); + + $rest = $this->getAuthService( self::API_FS_REPAIR); + $rest->POST([ + 'src' => $source_b64, + 'delete_archive' => $isToDelete, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Hash a file. This operation can take some time. To get the hash value, + * the returned task must have succeed and be in the state “done”. + * @see + * @param string $source : The file to hash + * @param string $hashType : The type of hash (md5, sha1, ...) - Default is md5 + * @return FsTask + */ + public function computeHash( $source, $hashType = 'md5'){ + /// Convert all paths in base64 + $source_b64 = base64_encode( $source); + + $rest = $this->getAuthService( self::API_FS_HASH); + $rest->POST([ + 'src' => $source_b64, + 'hash_type' => $hashType, + ]); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * Get the hash value + * To get the hash, the task must have succeed and be in the state “done”. + * @param $fsTaskId + * @return string + */ + public function getHashValue( $fsTaskId){ + $service = sprintf( self::API_FS_TASK_HASH, $fsTaskId); + $rest = $this->getAuthService( $service); + $rest->GET(); + + return $rest->getCurlResponse()['result']; + } + + /** + * Create a directory + * Contrary to other file system tasks, this operation is done synchronously. + * Instead of a returning a FsTask a call to this API will only return success status + * @param string $parentDirectory : The parent directory path + * @param string $newDirectoryName : The name of the directory to create + * @return bool + */ + public function createDirectory( $parentDirectory, $newDirectoryName){ + $rest = $this->getAuthService( self::API_FS_MKDIR); + $rest->POST([ + 'parent' => base64_encode( $parentDirectory), + 'dirname' => $newDirectoryName, + ]); + + return $rest->getCurlResponse()['success']; + } + + /** + * Rename a file/folder + * Contrary to other file system tasks, this operation is done synchronously. + * Instead of a returning a FsTask a call to this API will only return success status + * @param string $sourceFilePath : The source file path + * @param string $newFileName : The new name of the file (clear text, without path) + * @return bool + */ + public function rename( $sourceFilePath, $newFileName){ + $rest = $this->getAuthService( self::API_FS_RENAME); + $rest->POST([ + 'src' => base64_encode( $sourceFilePath), + 'dst' => $newFileName, + ]); + + return $rest->getCurlResponse()['success']; + } + + /** + * Download a file from the freebox server + * @param string $sourceFilePath + * @return mixed + */ + public function download( $sourceFilePath){ + $rest = $this->getAuthService( self::API_DOWNLOAD . base64_encode( $sourceFilePath), false, false); + $rest->GET( null, false); + + return $rest->getCurlResponse(); + } + +} diff --git a/freebox/api/v3/services/FileSystem/FileSystemTask.php b/freebox/api/v3/services/FileSystem/FileSystemTask.php new file mode 100644 index 0000000..ac82642 --- /dev/null +++ b/freebox/api/v3/services/FileSystem/FileSystemTask.php @@ -0,0 +1,72 @@ +getAuthService( self::API_FS_TASK); + $rest->GET(); + + $FsTask_xs = $rest->getCurlResponse()['result']; + $FsTasks = []; + foreach( $FsTask_xs as $fsTask_x) { + $FsTasks[] = new FsTask( $fsTask_x); + } + return $FsTasks; + } + + /** + * @param int $TaskId + * @return FsTask + */ + public function getTaskById( $TaskId){ + $rest = $this->getAuthService( self::API_FS_TASK . $TaskId); + $rest->GET(); + + return new FsTask( $rest->getCurlResponse()['result']); + } + + /** + * @param FsTask $FsTask + * @return bool + */ + public function deleteTask( FsTask $FsTask){ + return $this->deleteTaskById( $FsTask->getId()); + } + + /** + * @param int $TaskId + * @return bool + */ + public function deleteTaskById( $TaskId){ + $rest = $this->getAuthService( self::API_FS_TASK . $TaskId); + $rest->DELETE(); + + return $rest->getCurlResponse()['success']; + } + + /** + * @param FsTask $FsTask + * @return bool + */ + public function updateTask( FsTask $FsTask){ + $rest = $this->getAuthService( self::API_FS_TASK . $FsTask->getId()); + $rest->PUT( $FsTask->toArray()); + + return $rest->getCurlResponse()['success']; + } + +} diff --git a/freebox/api/v3/symbols/FileSystem/FileInfoType.php b/freebox/api/v3/symbols/FileSystem/FileInfoType.php new file mode 100644 index 0000000..cde4167 --- /dev/null +++ b/freebox/api/v3/symbols/FileSystem/FileInfoType.php @@ -0,0 +1,11 @@ +add_XFbxAppAuth_Header(); parent::GET( $curl_post_data); - $this->checkResponse(); + if( $checkResponse){ + $this->checkResponse(); + } } /**