Skip to content

Commit 4bce6e9

Browse files
committed
added file methods to be documented.
1 parent 9cf5c5b commit 4bce6e9

File tree

1 file changed

+106
-1
lines changed

1 file changed

+106
-1
lines changed

src/Resources/Files.php

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,31 @@
33
namespace Dcblogdev\MsGraph\Resources;
44

55
use Dcblogdev\MsGraph\Facades\MsGraph;
6-
use Exception;
6+
use GuzzleHttp\Client;
77

88
class Files extends MsGraph
99
{
10+
public function getFiles($path = null)
11+
{
12+
$path = $path === null ? 'me/drive/root/children?$orderby=name%20asc' : 'me/drive/root:'.$this->forceStartingSlash($path).':/children';
13+
return MsGraph::get($path);
14+
}
15+
16+
public function getDrive()
17+
{
18+
return MsGraph::get('me/drive');
19+
}
20+
1021
public function getDrives()
1122
{
1223
return MsGraph::get('me/drives');
1324
}
1425

26+
public function search($term)
27+
{
28+
return MsGraph::get("me/drive/root/search(q='$term')");
29+
}
30+
1531
public function downloadFile($id)
1632
{
1733
$id = MsGraph::get("me/drive/items/$id");
@@ -23,4 +39,93 @@ public function deleteFile($id)
2339
{
2440
return MsGraph::delete("me/drive/items/$id");
2541
}
42+
43+
public function createFolder($name, $path)
44+
{
45+
$path = $path === null ? 'me/drive/root/children' : 'me/drive/root:'.$this->forceStartingSlash($path).':/children';
46+
return MsGraph::post($path, [
47+
'name' => $name,
48+
'folder' => new \stdClass(),
49+
"@microsoft.graph.conflictBehavior" => "rename"
50+
]);
51+
}
52+
53+
public function getItem($id)
54+
{
55+
return MsGraph::get("me/drive/items/$id");
56+
}
57+
58+
public function rename($name, $id)
59+
{
60+
$path = "me/drive/items/$id";
61+
return MsGraph::patch($path, [
62+
'name' => $name
63+
]);
64+
}
65+
66+
public function upload($name, $uploadPath, $path = null)
67+
{
68+
$uploadSession = $this->createUploadSession($name, $path);
69+
$uploadUrl = $uploadSession['uploadUrl'];
70+
71+
$fragSize = 320 * 1024;
72+
$file = file_get_contents($uploadPath);
73+
$fileSize = strlen($file);
74+
$numFragments = ceil($fileSize / $fragSize);
75+
$bytesRemaining = $fileSize;
76+
$i = 0;
77+
$ch = curl_init($uploadUrl);
78+
while ($i < $numFragments) {
79+
$chunkSize = $numBytes = $fragSize;
80+
$start = $i * $fragSize;
81+
$end = $i * $fragSize + $chunkSize - 1;
82+
$offset = $i * $fragSize;
83+
if ($bytesRemaining < $chunkSize) {
84+
$chunkSize = $numBytes = $bytesRemaining;
85+
$end = $fileSize - 1;
86+
}
87+
if ($stream = fopen($uploadPath, 'r')) {
88+
// get contents using offset
89+
$data = stream_get_contents($stream, $chunkSize, $offset);
90+
fclose($stream);
91+
}
92+
93+
$content_range = " bytes " . $start . "-" . $end . "/" . $fileSize;
94+
$headers = [
95+
'Content-Length' => $numBytes,
96+
'Content-Range' => $content_range
97+
];
98+
99+
$client = new Client;
100+
$response = $client->put($uploadUrl, [
101+
'headers' => $headers,
102+
'body' => $data,
103+
]);
104+
105+
$bytesRemaining = $bytesRemaining - $chunkSize;
106+
$i++;
107+
}
108+
109+
}
110+
111+
protected function createUploadSession($name, $path = null)
112+
{
113+
$path = $path === null ? "me/drive/root:/$name:/createUploadSession" : "me/drive/root:".$this->forceStartingSlash($path)."/$name:/createUploadSession";
114+
115+
return MsGraph::post($path, [
116+
'item' => [
117+
"@microsoft.graph.conflictBehavior" => "rename",
118+
"name" => $name
119+
]
120+
]);
121+
}
122+
123+
protected function forceStartingSlash($string)
124+
{
125+
if (substr($string, 0, 1) !== "/") {
126+
$string = "/$string";
127+
}
128+
129+
return $string;
130+
}
26131
}

0 commit comments

Comments
 (0)