Skip to content

Commit f05435b

Browse files
uploader
1 parent ba5356f commit f05435b

5 files changed

Lines changed: 464 additions & 4 deletions

File tree

src/OpenApi.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ private function getListaModuli(){
106106

107107
$moduli['ws.pecmassiva.com'] = "\\OpenApi\\classes\\PecMassiva";
108108
$nomi['ws.pecmassiva.com'] = "pecMassiva";
109+
110+
$moduli['uploader.altravia.com'] = "\\OpenApi\\classes\\Uploader";
111+
$nomi['uploader.altravia.com'] = "uploader";
109112
return array($moduli,$nomi);
110113
}
111114

src/classes/OpenApiBase.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private function checkHasScope(string $url, string $type){
8989
*
9090
* @return mixed
9191
*/
92-
protected function connect(string $endpoint, $type = "GET", $param = [], $ttr = 0, $force = false, $addHeader = NULL){
92+
public function connect(string $endpoint, $type = "GET", $param = [], $ttr = 0, $force = false, $addHeader = NULL){
9393
$url = $this->basePath;
9494
$url = str_replace("https://","https://".$this->prefix,$url);
9595
$url = str_replace("http://","http://".$this->prefix,$url);
@@ -132,15 +132,27 @@ protected function connect(string $endpoint, $type = "GET", $param = [], $ttr =
132132
$this->rawResponse = $response;
133133
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
134134
$this->header = substr($response, 0, $header_size);
135+
//var_dump($this->header);exit;
136+
$this->parsedHEader = $this->parseHeader($this->header);
135137
$return = substr($response, $header_size);
136138
$httpCode = curl_getinfo ( $ch, CURLINFO_RESPONSE_CODE );;
137139
curl_close($ch);
138-
$data = json_decode($return);
140+
141+
if(isset($this->parsedHEader['Content-Type']) && strtolower($this->parsedHEader['Content-Type']) == "application/json") {
142+
143+
$data = json_decode($return);
144+
}else if(json_decode($return) != NULL){
145+
$data = json_decode($return);
146+
147+
}else{
148+
$data = $return;
149+
}
150+
139151
if($data == NULL){
140152
throw new \OpenApi\classes\exception\OpenApiConnectionsException("Connection to $url: Connection Error",40001);
141153
}
142-
// var_dump($data);exit;
143-
if($data->success == false){
154+
155+
if(is_object($data) && $data->success == false){
144156
$message = "Connection to $url: unknow error";
145157
if(isset($data->message)) {
146158
if(is_string(($data->message))){
@@ -166,4 +178,25 @@ protected function connect(string $endpoint, $type = "GET", $param = [], $ttr =
166178
return $data;
167179
}
168180

181+
private function parseHeader($headers){
182+
$headers = explode("\n",$headers);
183+
$parsedHeaders = array();
184+
foreach ($headers as $header) {
185+
$header = trim($header);
186+
187+
$header = explode(":",$header);
188+
189+
if(count($header) < 2){
190+
191+
continue;
192+
}
193+
$key = $header[0];
194+
unset($header[0]);
195+
$parsedHeaders[trim($key)] = trim(implode(":",$header));
196+
}
197+
// exit;
198+
199+
return $parsedHeaders;
200+
}
201+
169202
}

src/classes/Uploader.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
namespace OpenApi\classes;
3+
class Uploader extends OpenApiBase {
4+
5+
/**
6+
* @param string $token Il token da utilizzare per il collegamento
7+
* @param array $scopes Array con la lista degli scope per cui il token è abilitato
8+
* @param object $cache Classe che gestisce la cahce, deve essere una classe che estende {@see OpenApi\clasess\utility\DummyCache} o comunque compatibile con essa (stessi metodi)
9+
*/
10+
function __construct(string $token, array $scopes, object $cache, string $prefix){
11+
parent::__construct($token, $scopes, $cache, $prefix);
12+
$this->basePath = "https://uploader.altravia.com";
13+
}
14+
15+
function newCollection(){
16+
17+
return new \OpenApi\classes\utility\Uploader\Collection([$this, 'connect']);
18+
}
19+
20+
function gateway(){
21+
22+
$endpoint = isset($_GET['endpoint'])?$_GET['endpoint']:null;
23+
if($endpoint == NULL){
24+
throw new \OpenApi\classes\exception\OpenApiUploaderException("No endpoint GET",40018);
25+
}
26+
27+
$method = $_SERVER['REQUEST_METHOD'];
28+
$data = null;
29+
if($method != "GET" && $method != "DELETE"){
30+
$data = file_get_contents("php://input");
31+
$data = json_decode($data);
32+
}
33+
$data = $this->connect($endpoint, $method,$data);
34+
header("Content-Type: ",$this->parsedHEader['Content-Type']);
35+
if(isset($this->parsedHEader['Content-Type']) && strtolower($this->parsedHEader['Content-Type']) == "application/json") {
36+
echo json_encode($data);
37+
}else{
38+
echo $data;
39+
}
40+
}
41+
42+
function createCollection($outputFormat, $outputSize = null, $inputTypes = null, $inputCount = null, $public = null, $watermark = null, $watermarkPosition = NULL, $expireTimestamp = null) {
43+
$collection = new \OpenApi\classes\utility\Uploader\Collection([$this, 'connect']);
44+
$collection->setOutput($outputFormat);
45+
$collection->setOutputSize($outputSize);
46+
$collection->setInputTypes($inputTypes);
47+
$collection->setInputCount($inputCount);
48+
$collection->setPublic($public);
49+
$collection->setWaterMark($watermark);
50+
$collection->setWaterMarkPosition($watermarkPosition);
51+
$collection->setExpireTimestamp($expireTimestamp);
52+
$collection->save();
53+
}
54+
55+
function getCollectionById($id){
56+
$coll = $this->connect("collections/$id","GET");
57+
$collection = new \OpenApi\classes\utility\Uploader\Collection([$this, 'connect']);
58+
if(isset($coll->data)){
59+
$collection->parseData($coll->data);
60+
return $collection;
61+
}
62+
return null;
63+
}
64+
65+
function deleteCollectionById($id){
66+
return $this->connect("collections/$id","DELETE");
67+
}
68+
69+
function getCollections(){
70+
$collections = $this->connect("collections","GET");
71+
foreach($collections->data as $d){
72+
$d->id =$d->_id->{'$oid'};
73+
unset($d->_id);
74+
}
75+
return $collections;
76+
}
77+
78+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace OpenApi\classes\exception;
3+
4+
/**
5+
* Gestisce le eccezioni relative alle funzionalità Uploader
6+
* 400013: Si è tentato di impostare un output diverso da uno di quelli utilizzabili
7+
* 400014: Si è tentato di impostare un output size non compatibile con l'output format
8+
* 40015: Si è tentato di impostare a true output group con output format di tipo immagine
9+
* 40016: Si è tentato di impostare un input type non valido
10+
* 40017: Si è tentato di salvare una collection senza aver settato output format
11+
* 40018: Si è invocato il gateway senza endpoint in GET
12+
* 40019: Si è tentato di aggiungere un file ad una collection ancora non salvata
13+
*/
14+
class OpenApiUploaderException extends OpenApiExceptionBase
15+
{
16+
public function __construct($message, $code = 0, \Exception $previous = null) {
17+
parent::__construct($message, $code, $previous);
18+
}
19+
}

0 commit comments

Comments
 (0)