Skip to content

Commit fb31256

Browse files
committed
feat: upload to cos
1 parent 0ae1fe5 commit fb31256

File tree

6 files changed

+379
-86
lines changed

6 files changed

+379
-86
lines changed

app/Coding.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Client;
66
use Illuminate\Support\Facades\Log;
7+
use Illuminate\Support\Facades\Storage;
78

89
class Coding
910
{
@@ -41,7 +42,7 @@ public function createUploadToken($token, $projectName, $fileName)
4142
'Content-Type' => 'application/json'
4243
],
4344
'json' => [
44-
'Action' => 'CreateWiki',
45+
'Action' => 'CreateUploadToken',
4546
'ProjectName' => $projectName,
4647
'FileName' => $fileName,
4748
],
@@ -52,17 +53,19 @@ public function createUploadToken($token, $projectName, $fileName)
5253
$uploadToken['UploadLink'],
5354
$matches
5455
);
55-
$uploadToken['Bucket'] = $matches[1][0];
56+
$uploadToken['Bucket'] = $matches[1][0] . '-' . $matches[2][0];
5657
$uploadToken['AppId'] = $matches[2][0];
5758
$uploadToken['Region'] = $matches[3][0];
5859
return $uploadToken;
5960
}
6061

6162
public function createMarkdownZip($markdown, $path, $filename): bool|string
6263
{
63-
$zipFilename = tempnam(sys_get_temp_dir(), $filename);
64-
if ($this->zipArchive->open($zipFilename, \ZipArchive::OVERWRITE) !== true) {
65-
Log::error("cannot open <$zipFilename>");
64+
$tmpFile = tempnam(sys_get_temp_dir(), $filename);
65+
$zipFileFullPath = $tmpFile . '.zip';
66+
rename($tmpFile, $zipFileFullPath);
67+
if ($this->zipArchive->open($zipFileFullPath, \ZipArchive::OVERWRITE) !== true) {
68+
Log::error("cannot open <$zipFileFullPath>");
6669
return false;
6770
}
6871
$this->zipArchive->addFromString($filename, $markdown);
@@ -73,6 +76,18 @@ public function createMarkdownZip($markdown, $path, $filename): bool|string
7376
}
7477
}
7578
$this->zipArchive->close();
76-
return $zipFilename;
79+
return $zipFileFullPath;
80+
}
81+
82+
public function upload(array $uploadToken, string $fileFullPath): bool
83+
{
84+
config(['filesystems.disks.cos.credentials.appId' => $uploadToken['AppId']]);
85+
config(['filesystems.disks.cos.credentials.secretId' => $uploadToken['SecretId']]);
86+
config(['filesystems.disks.cos.credentials.secretKey' => $uploadToken['SecretKey']]);
87+
config(['filesystems.disks.cos.credentials.token' => $uploadToken['UpToken']]);
88+
config(['filesystems.disks.cos.region' => $uploadToken['Region']]);
89+
config(['filesystems.disks.cos.bucket' => $uploadToken['Bucket']]);
90+
91+
return Storage::disk('cos')->put(basename($fileFullPath), $fileFullPath);
7792
}
7893
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"ext-zip": "*",
2424
"laravel-fans/confluence": "^0.1.1",
2525
"laravel-zero/framework": "^8.8",
26-
"league/html-to-markdown": "^5.0"
26+
"league/html-to-markdown": "^5.0",
27+
"sinkcup/laravel-filesystem-cos-updated": "^4.0"
2728
},
2829
"require-dev": {
2930
"fakerphp/faker": "^1.14",

0 commit comments

Comments
 (0)