This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Imfs.php
99 lines (89 loc) · 3.21 KB
/
Imfs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Service
*/
namespace ZendService\Nirvanix\Context;
use Zend\Http\Request as HttpRequest;
use ZendService\Nirvanix\Response;
/**
* Namespace proxy with additional convenience methods for the IMFS namespace.
*
* @category Zend
* @package Zend_Service
* @subpackage Nirvanix
*/
class Imfs extends Base
{
/**
* Convenience function to get the contents of a file on
* the Nirvanix IMFS. Analog to PHP's file_get_contents().
*
* @param string $filePath Remote path and filename
* @param integer $expiration Number of seconds that Nirvanix
* make the file available for download.
* @return string Contents of file
*/
public function getContents($filePath, $expiration = 3600)
{
// get url to download the file
$params = array(
'filePath' => $filePath,
'expiration' => $expiration,
);
$resp = $this->getOptimalUrls($params);
$url = (string)$resp->Download->DownloadURL;
// download the file
$client = $this->httpClient;
$client->resetParameters();
$client->setUri($url);
$client->setMethod(HttpRequest::METHOD_GET);
$resp = $client->send();
return $resp->getBody();
}
/**
* Convenience function to put the contents of a string into
* the Nirvanix IMFS. Analog to PHP's file_put_contents().
*
* @param string $filePath Remote path and filename
* @param integer $data Data to store in the file
* @param string $mimeType Mime type of data
* @return Response
*/
public function putContents($filePath, $data, $mimeType = null)
{
// get storage node for upload
$params = array('sizeBytes' => strlen($data));
$resp = $this->getStorageNode($params);
$host = (string) $resp->GetStorageNode->UploadHost;
$uploadToken = (string) $resp->GetStorageNode->UploadToken;
$client = $this->httpClient;
// http upload data into remote file
$client->resetParameters();
$client->setUri("http://{$host}/Upload.ashx");
$client->setMethod(HttpRequest::METHOD_POST);
$client->setParameterPost(array(
'uploadToken' => $uploadToken,
'destFolderPath' => str_replace('\\', '/',dirname($filePath)),
));
$client->setFileUpload(basename($filePath), 'uploadFile', $data, $mimeType);
$response = $client->send();
return new Response($response->getBody());
}
/**
* Convenience function to remove a file from the Nirvanix IMFS.
* Analog to PHP's unlink().
*
* @param string $filePath Remove path and filename
* @return Response
*/
public function unlink($filePath)
{
$params = array('filePath' => $filePath);
return $this->deleteFiles($params);
}
}