-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDisk.php
More file actions
96 lines (92 loc) · 3.32 KB
/
Disk.php
File metadata and controls
96 lines (92 loc) · 3.32 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
<?php
namespace App\Coding;
class Disk extends Base
{
/**
* 创建网盘目录,不可重名,如已存在,仍然正常返回 id
*
* @param string $token
* @param string $projectName
* @param string $folderName
* @param int $parentId
* @return int
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function createFolder(string $token, string $projectName, string $folderName, int $parentId = 0): int
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => [
'Action' => 'CreateFolder',
'ProjectName' => $projectName,
'FolderName' => $folderName,
'ParentId' => $parentId,
],
]);
$result = json_decode($response->getBody(), true);
return $result['Response']['Data']['Id'];
}
/**
* @param string $token
* @param string $projectName
* @param array $data
* @return int
* @throws \GuzzleHttp\Exception\GuzzleException
* @todo data 数组无法强类型校验内部字段,考虑用对象
*/
public function createFile(string $token, string $projectName, array $data): array
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'Action' => 'CreateFile',
'ProjectName' => $projectName,
], $data),
]);
$result = json_decode($response->getBody(), true);
return $result['Response']['Data'];
}
public function uploadAttachments(string $token, string $projectName, string $dataDir, array $attachments): array
{
if (empty($attachments)) {
return [];
}
$data = [];
// TODO hard code folder name
$folderId = $this->createFolder($token, $projectName, 'wiki-attachments');
foreach ($attachments as $path => $filename) {
$uploadToken = $this->createUploadToken(
$token,
$projectName,
$filename
);
$filePath = $dataDir . DIRECTORY_SEPARATOR . $path;
$result = [];
try {
$this->upload($uploadToken, $filePath);
$result = $this->createFile($token, $projectName, [
"OriginalFileName" => $filename,
"MimeType" => mime_content_type($filePath),
"FileSize" => filesize($filePath),
"StorageKey" => $uploadToken['StorageKey'],
"Time" => $uploadToken['Time'],
"AuthToken" => $uploadToken['AuthToken'],
"FolderId" => $folderId,
]);
} catch (\Exception $e) {
// TODO laravel log
error_log('ERROR: ' . $e->getMessage());
}
$data[$path] = $result;
}
return $data;
}
}