3
3
namespace Dcblogdev \MsGraph \Resources ;
4
4
5
5
use Dcblogdev \MsGraph \Facades \MsGraph ;
6
- use Exception ;
6
+ use GuzzleHttp \ Client ;
7
7
8
8
class Files extends MsGraph
9
9
{
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
+
10
21
public function getDrives ()
11
22
{
12
23
return MsGraph::get ('me/drives ' );
13
24
}
14
25
26
+ public function search ($ term )
27
+ {
28
+ return MsGraph::get ("me/drive/root/search(q=' $ term') " );
29
+ }
30
+
15
31
public function downloadFile ($ id )
16
32
{
17
33
$ id = MsGraph::get ("me/drive/items/ $ id " );
@@ -23,4 +39,93 @@ public function deleteFile($id)
23
39
{
24
40
return MsGraph::delete ("me/drive/items/ $ id " );
25
41
}
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
+ }
26
131
}
0 commit comments