Add FileUpload API

This commit is contained in:
alphayax 2016-05-22 18:15:08 +02:00
parent a2967cae91
commit 2808fa136d
6 changed files with 304 additions and 0 deletions

View File

@ -24,6 +24,7 @@ Jusqu'a présent, les fonctionalités suivantes ont été implémentées :
- FsTask
- FsOperation
- FsListing
- FileUpload
- FileSharing
- Downloads
- Download

43
TODO.md Normal file
View File

@ -0,0 +1,43 @@
- Downloads
- ~~Download~~
- Download Stats
- Download Files
- Download Trackers [UNSTABLE]
- Download Peers [UNSTABLE]
- Download Blacklist [UNSTABLE]
- Download Feeds
- Download Configuration
- ~~File System Api~~
- ~~File System~~
- ~~File Sharing Link~~
- ~~File Upload~~
- ~~Air Media~~
- ~~AirMedia API~~
- RRD
- RRD [UNSTABLE]
- ~~Calls / Contacts~~
- ~~Call~~
- ~~Contacts~~
- Configuration
- ~~Connection API~~
- ~~Lan~~
- ~~Lan Browser~~
- ~~Freeplug~~
- ~~DHCP~~
- ~~Ftp~~
- ~~NAT~~
- ~~Port Forwarding~~
- ~~Incoming port configuration~~
- UPnP IGD
- ~~LCD~~
- Network Share
- UPnP AV
- Switch
- Wi-Fi
- ~~System~~
- VPN Server [UNSTABLE]
- VPN Client [UNSTABLE]
- Storage
- Storage API [UNSTABLE]
- Parental filter
- Parental Control

View File

@ -0,0 +1,20 @@
<?php
/// Require Composer AutoLoader
require_once '../../vendor/autoload.php';
/// Define our application
$App = new \alphayax\freebox\utils\Application( 'com.alphayax.freebox.filesystem', 'Freebox PHP API Example (FileSystem)', '1.0.0');
$App->authorize();
$App->openSession();
$FileUploadService = new \alphayax\freebox\api\v3\services\FileSystem\FileUpload( $App);
/*
$authID = $FileUploadService->createAuthorization('/Disque dur/Photos/', 'P3140017.jpg');
$success = $FileUploadService->uploadFile( $authID, '/home/alphayax/Bureau/P3140017.jpg');
print_r( $success); // 1939839620
*/
$AllUploads = $FileUploadService->getAll();
print_r( $AllUploads);

View File

@ -0,0 +1,87 @@
<?php
namespace alphayax\freebox\api\v3\models\FileSystem;
use alphayax\freebox\api\v3\Model;
/**
* Class FileUpload
* @package alphayax\freebox\api\v3\models\FileSystem
*/
class FileUpload extends Model {
/** @var string (Read-only) : upload id */
protected $id;
/** @var int (Read-only) : Upload file size in bytes */
protected $size;
/** @var int (Read-only) : Uploaded bytes */
protected $uploaded;
/**
* @var string enum (Read-only) : upload status can have the following values
* @see alphayax\freebox\api\v3\symbols\FileSystem\FileUploadStatus
*/
protected $status;
/** @var int timestamp (Read-only) : last update of file upload object */
protected $last_update;
/** @var string (Read-only) : name of the file uploaded */
protected $upload_name;
/** @var string (Read-only) : upload destination directory */
protected $dirname;
/**
* @return string
*/
public function getId(){
return $this->id;
}
/**
* @return int
*/
public function getSize(){
return $this->size;
}
/**
* @return int
*/
public function getUploaded(){
return $this->uploaded;
}
/**
* @return string
* @see alphayax\freebox\api\v3\symbols\FileSystem\FileUploadStatus
*/
public function getStatus(){
return $this->status;
}
/**
* @return int
*/
public function getLastUpdate(){
return $this->last_update;
}
/**
* @return string
*/
public function getUploadName(){
return $this->upload_name;
}
/**
* @return string
*/
public function getDirname(){
return $this->dirname;
}
}

View File

@ -0,0 +1,116 @@
<?php
namespace alphayax\freebox\api\v3\services\FileSystem;
use alphayax\freebox\api\v3\models;
use alphayax\freebox\api\v3\Service;
/**
* Class FileUpload
* @package alphayax\freebox\api\v3\services\FileSystem
*/
class FileUpload extends Service {
const API_UPLOAD = '/api/v3/upload/';
const API_UPLOAD_SEND = '/api/v3/upload/%u/send';
const API_UPLOAD_CANCEL = '/api/v3/upload/%u/cancel';
const API_UPLOAD_CLEAN = '/api/v3/upload/clean';
/**
* Create a file upload authorization
* @param string $dirName
* @param string $FileName
* @return int The new upload authorization id
*/
public function createAuthorization( $dirName, $FileName){
$rest = $this->getAuthService( self::API_UPLOAD);
$rest->POST([
'dirname' => base64_encode( $dirName),
'upload_name' => $FileName,
]);
return (int) $rest->getCurlResponse()['result']['id'];
}
/**
* Send the content of the FileUpload task
* @param int $FileUploadTaskId
* @param string $fileToUpload_afi
* @return bool
*/
public function uploadFile( $FileUploadTaskId, $fileToUpload_afi){
$Service = sprintf( self::API_UPLOAD_SEND, $FileUploadTaskId);
$rest = $this->getAuthService( $Service);
$rest->setContentType_MultipartFormData();
$rest->POST([
basename( $fileToUpload_afi) => new \CurlFile( $fileToUpload_afi),
]);
return $rest->getCurlResponse()['success'];
}
/**
* Get the list of uploads
* @return models\FileSystem\FileUpload[]
*/
public function getAll(){
$rest = $this->getAuthService( self::API_UPLOAD);
$rest->GET();
$FileUpload_xs = @$rest->getCurlResponse()['result'] ?: [];
$FileUploads = [];
foreach( $FileUpload_xs as $FileUpload_x) {
$FileUploads[] = new models\FileSystem\FileUpload( $FileUpload_x);
}
return $FileUploads;
}
/**
* Track an upload status
* @param int $FileUploadId
* @return models\FileSystem\FileUpload
*/
public function getFromId( $FileUploadId){
$rest = $this->getAuthService( self::API_UPLOAD . $FileUploadId);
$rest->GET();
return new models\FileSystem\FileUpload( $rest->getCurlResponse()['result']);
}
/**
* Cancel the given FileUpload closing the connection
* The upload status must be in_progress
* @param int $FileUploadId
* @return bool
*/
public function cancelFromId( $FileUploadId){
$Service = sprintf( self::API_UPLOAD_CANCEL, $FileUploadId);
$rest = $this->getAuthService( $Service);
$rest->DELETE();
return $rest->getCurlResponse()['success'];
}
/**
* Delete the given FileUpload closing the connection if needed
* @param int $FileUploadId
* @return bool
*/
public function deleteFromId( $FileUploadId){
$rest = $this->getAuthService( self::API_UPLOAD . $FileUploadId);
$rest->DELETE();
return $rest->getCurlResponse()['success'];
}
/**
* Deletes all the FileUpload not in_progress
* @return bool
*/
public function cleanTerminated(){
$rest = $this->getAuthService( self::API_UPLOAD_CLEAN);
$rest->DELETE();
return $rest->getCurlResponse()['success'];
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace alphayax\freebox\api\v3\symbols\FileSystem;
/**
* Interface FileUploadStatus
* @package alphayax\freebox\api\v3\symbols\FileSystem
*/
interface FileUploadStatus {
/** Upload authorization is valid, upload has not started yet */
CONST AUTHORIZED = 'authorized';
/** Upload in progress */
CONST IN_PROGRESS = 'in_progress';
/** Upload done */
CONST DONE = 'done';
/** Upload failed */
CONST FAILED = 'failed';
/** Destination file conflict */
CONST CONFLICT = 'conflict';
/** Upload authorization is no longer valid */
CONST TIMEOUT = 'timeout';
/** Upload cancelled by user */
CONST CANCELLED = 'cancelled';
/** timestamp Read-only */
CONST START_DATE = 'start_date';
/** start date */
CONST UPLOAD = 'upload';
}