-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLargeFileUpload.php
More file actions
120 lines (101 loc) · 4.91 KB
/
Copy pathLargeFileUpload.php
File metadata and controls
120 lines (101 loc) · 4.91 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
use Microsoft\Graph\Core\Tasks\LargeFileUploadTask;
use Microsoft\Graph\Generated\Drives\Item\Items\Item\CreateUploadSession\CreateUploadSessionPostRequestBody as DriveItemCreateUploadSessionPostRequestBody;
use Microsoft\Graph\Generated\Models;
use Microsoft\Graph\Generated\Users\Item\Messages\Item\Attachments\CreateUploadSession\CreateUploadSessionPostRequestBody as AttachmentCreateUploadSessionPostRequestBody;
use Microsoft\Graph\GraphServiceClient;
class LargeFileUpload {
public static function runAllSamples(GraphServiceClient $graphClient): void {
$filePath = $_ENV['LARGE_FILE_PATH'];
LargeFileUpload::uploadFileToOneDrive($graphClient, $filePath, 'Documents/vacation.gif');
LargeFileUpload::uploadAttachmentToMessage($graphClient, $filePath);
}
private static function uploadFileToOneDrive(GraphServiceClient $graphClient, string $filePath, string $itemPath): void {
// <LargeFileUploadSnippet>
// Create a file stream
$file = GuzzleHttp\Psr7\Utils::streamFor(fopen($filePath, 'r'));
// Create the upload session request
$uploadProperties = new Models\DriveItemUploadableProperties();
$uploadProperties->setAdditionalData([
'@microsoft.graph.conflictBehavior' => 'replace'
]);
// use Microsoft\Graph\Generated\Drives\Item\Items\Item\CreateUploadSession\CreateUploadSessionPostRequestBody
// as DriveItemCreateUploadSessionPostRequestBody;
$uploadSessionRequest = new DriveItemCreateUploadSessionPostRequestBody();
$uploadSessionRequest->setItem($uploadProperties);
// Create the upload session
/** @var Models\Drive $drive */
$drive = $graphClient->me()->drive()->get()->wait();
$uploadSession = $graphClient->drives()
->byDriveId($drive->getId())
->items()
->byDriveItemId('root:/'.$itemPath.':')
->createUploadSession()
->post($uploadSessionRequest)
->wait();
$largeFileUpload = new LargeFileUploadTask($uploadSession, $graphClient->getRequestAdapter(), $file);
$totalSize = $file->getSize();
$progress = function(array $prog) use ($totalSize): void {
echo 'Uploaded '.$prog[1].' of '.$totalSize.' bytes'.PHP_EOL;
};
try {
$largeFileUpload->upload($progress)->wait();
} catch (\Psr\Http\Client\NetworkExceptionInterface $ex) {
$largeFileUpload->resume()->wait();
}
// </LargeFileUploadSnippet>
// Added to remove warning about unused function
if (is_null($uploadSession)) {
LargeFileUpload::resumeUpload($largeFileUpload);
}
}
private static function resumeUpload(LargeFileUploadTask $largeFileUpload): void {
// <ResumeSnippet>
$largeFileUpload->resume();
// </ResumeSnippet>
}
private static function uploadAttachmentToMessage(GraphServiceClient $graphClient, string $filePath): void {
// <UploadAttachmentSnippet>
// Create a message
$draftMessage = new Models\Message();
$draftMessage->setSubject('Large attachment');
/** @var Models\Message $savedDraft */
$savedDraft = $graphClient->me()
->messages()
->post($draftMessage)
->wait();
// Create a file stream
$file = GuzzleHttp\Psr7\Utils::streamFor(fopen($filePath, 'r'));
// Create an attachment
$attachment = new Models\AttachmentItem();
$attachment->setAttachmentType(new Models\AttachmentType(Models\AttachmentType::FILE));
$attachment->setName(basename($filePath));
$attachment->setSize($file->getSize());
// use Microsoft\Graph\Generated\Users\Item\Messages\Item\Attachments\CreateUploadSession\CreateUploadSessionPostRequestBody
// as AttachmentCreateUploadSessionPostRequestBody;
$uploadSessionRequest = new AttachmentCreateUploadSessionPostRequestBody();
$uploadSessionRequest->setAttachmentItem($attachment);
// Create the upload session
$uploadSession = $graphClient->me()
->messages()
->byMessageId($savedDraft->getId())
->attachments()
->createUploadSession()
->post($uploadSessionRequest)
->wait();
$largeFileUpload = new LargeFileUploadTask($uploadSession, $graphClient->getRequestAdapter(), $file);
$totalSize = $file->getSize();
$progress = function(array $prog) use ($totalSize): void {
echo 'Uploaded '.$prog[1].' of '.$totalSize.' bytes'.PHP_EOL;
};
try {
$largeFileUpload->upload($progress)->wait();
} catch (\Psr\Http\Client\NetworkExceptionInterface $ex) {
$largeFileUpload->resume()->wait();
}
// </UploadAttachmentSnippet>
}
}
?>