From 366c52ae3865c70e1d1a19b6d107adce3e51adb9 Mon Sep 17 00:00:00 2001
From: zds <49744633+zds-s@users.noreply.github.com>
Date: Mon, 7 Oct 2024 22:03:32 +0800
Subject: [PATCH] Initial 3.0 Component
---
.gitattributes | 2 +
.github/workflows/close-pull-request.yml | 15 +++
.github/workflows/release.yml | 25 +++++
.gitignore | 5 +
Event/UploadEvent.php | 44 +++++++++
Event/UploadedEvent.php | 27 ++++++
Exception/UploadFailException.php | 28 ++++++
Factory.php | 34 +++++++
LICENSE | 21 ++++
Listener/UploadListener.php | 72 ++++++++++++++
README.md | 3 +
Upload.php | 118 +++++++++++++++++++++++
UploadInterface.php | 23 +++++
composer.json | 28 ++++++
14 files changed, 445 insertions(+)
create mode 100644 .gitattributes
create mode 100644 .github/workflows/close-pull-request.yml
create mode 100644 .github/workflows/release.yml
create mode 100644 .gitignore
create mode 100644 Event/UploadEvent.php
create mode 100644 Event/UploadedEvent.php
create mode 100644 Exception/UploadFailException.php
create mode 100644 Factory.php
create mode 100644 LICENSE
create mode 100644 Listener/UploadListener.php
create mode 100644 README.md
create mode 100644 Upload.php
create mode 100644 UploadInterface.php
create mode 100644 composer.json
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..ce244ab
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1,2 @@
+/tests export-ignore
+/.github export-ignore
\ No newline at end of file
diff --git a/.github/workflows/close-pull-request.yml b/.github/workflows/close-pull-request.yml
new file mode 100644
index 0000000..7239c0b
--- /dev/null
+++ b/.github/workflows/close-pull-request.yml
@@ -0,0 +1,15 @@
+name: Close Pull Request
+
+permissions: write-all
+
+on:
+ pull_request_target:
+ types: [ opened ]
+
+jobs:
+ run:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: superbrothers/close-pull-request@v3
+ with:
+ comment: "Hi, this is a READ-ONLY repository, please submit your PR on the https://github.com/mineadmin/components repository.
This Pull Request will close automatically.
Thanks! "
\ No newline at end of file
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
new file mode 100644
index 0000000..ae1823d
--- /dev/null
+++ b/.github/workflows/release.yml
@@ -0,0 +1,25 @@
+on:
+ push:
+ tags:
+ - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
+
+name: Release
+permissions: write-all
+
+jobs:
+ release:
+ name: Release
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout code
+ uses: actions/checkout@v4
+ - name: Create Release
+ id: create_release
+ uses: actions/create-release@v1
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ with:
+ tag_name: ${{ github.ref }}
+ release_name: Release ${{ github.ref }}
+ draft: false
+ prerelease: false
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..7cb10a6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+composer.lock
+.idea
+vendor
+*.cache
+runtime
\ No newline at end of file
diff --git a/Event/UploadEvent.php b/Event/UploadEvent.php
new file mode 100644
index 0000000..6d1c94a
--- /dev/null
+++ b/Event/UploadEvent.php
@@ -0,0 +1,44 @@
+uploadFile;
+ }
+
+ public function setUpload(?Upload $upload): void
+ {
+ $this->upload = $upload;
+ }
+
+ public function getUpload(): ?Upload
+ {
+ return $this->upload;
+ }
+
+ public function isUploaded(): bool
+ {
+ return $this->upload !== null;
+ }
+}
diff --git a/Event/UploadedEvent.php b/Event/UploadedEvent.php
new file mode 100644
index 0000000..858c8a3
--- /dev/null
+++ b/Event/UploadedEvent.php
@@ -0,0 +1,27 @@
+upload;
+ }
+}
diff --git a/Exception/UploadFailException.php b/Exception/UploadFailException.php
new file mode 100644
index 0000000..e7b4b3d
--- /dev/null
+++ b/Exception/UploadFailException.php
@@ -0,0 +1,28 @@
+event;
+ }
+}
diff --git a/Factory.php b/Factory.php
new file mode 100644
index 0000000..3b2ec60
--- /dev/null
+++ b/Factory.php
@@ -0,0 +1,34 @@
+dispatcher->dispatch($event);
+ if ($event->isUploaded()) {
+ return $event->getUpload();
+ }
+ throw new UploadFailException($event);
+ }
+}
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..2874484
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2024 MineAdmin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/Listener/UploadListener.php b/Listener/UploadListener.php
new file mode 100644
index 0000000..6f4c113
--- /dev/null
+++ b/Listener/UploadListener.php
@@ -0,0 +1,72 @@
+filesystem = $filesystemFactory->get(static::ADAPTER_NAME);
+ }
+
+ public function listen(): array
+ {
+ return [
+ UploadEvent::class,
+ ];
+ }
+
+ public function process(object $event): void
+ {
+ if ($event instanceof UploadEvent) {
+ $fileInfo = $event->getUploadFile();
+ $path = $this->generatorPath();
+ $filename = $this->generatorId() . '.' . Str::lower($fileInfo->getExtension());
+ $this->filesystem->write($path . '/' . $filename, file_get_contents($fileInfo->getRealPath()));
+ $event->setUpload(new Upload(
+ static::ADAPTER_NAME,
+ $filename,
+ mime_content_type($fileInfo->getRealPath()),
+ $path,
+ md5_file($fileInfo->getRealPath()),
+ Str::lower($fileInfo->getExtension()),
+ $fileInfo->getSize(),
+ $fileInfo->getSize(),
+ $this->filesystem->publicUrl($path . '/' . $filename)
+ ));
+ }
+ }
+
+ protected function generatorPath(): string
+ {
+ return date('Y-m-d');
+ }
+
+ protected function generatorId(): string
+ {
+ return Uuid::uuid4()->toString();
+ }
+}
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..be139d4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# MineAdmin
+
+[Documentation](https://doc.mineadmin.com)
\ No newline at end of file
diff --git a/Upload.php b/Upload.php
new file mode 100644
index 0000000..fb47b08
--- /dev/null
+++ b/Upload.php
@@ -0,0 +1,118 @@
+storageMode;
+ }
+
+ public function setStorageMode(string $storageMode): void
+ {
+ $this->storageMode = $storageMode;
+ }
+
+ public function getObjectName(): string
+ {
+ return $this->objectName;
+ }
+
+ public function setObjectName(string $objectName): void
+ {
+ $this->objectName = $objectName;
+ }
+
+ public function getMimeType(): string
+ {
+ return $this->mimeType;
+ }
+
+ public function setMimeType(string $mimeType): void
+ {
+ $this->mimeType = $mimeType;
+ }
+
+ public function getStoragePath(): string
+ {
+ return $this->storagePath;
+ }
+
+ public function setStoragePath(string $storagePath): void
+ {
+ $this->storagePath = $storagePath;
+ }
+
+ public function getHash(): string
+ {
+ return $this->hash;
+ }
+
+ public function setHash(string $hash): void
+ {
+ $this->hash = $hash;
+ }
+
+ public function getSuffix(): string
+ {
+ return $this->suffix;
+ }
+
+ public function setSuffix(string $suffix): void
+ {
+ $this->suffix = $suffix;
+ }
+
+ public function getSizeByte(): int
+ {
+ return $this->sizeByte;
+ }
+
+ public function setSizeByte(int $sizeByte): void
+ {
+ $this->sizeByte = $sizeByte;
+ }
+
+ public function getSizeInfo(): int
+ {
+ return $this->sizeInfo;
+ }
+
+ public function setSizeInfo(int $sizeInfo): void
+ {
+ $this->sizeInfo = $sizeInfo;
+ }
+
+ public function getUrl(): string
+ {
+ return $this->url;
+ }
+
+ public function setUrl(string $url): void
+ {
+ $this->url = $url;
+ }
+}
diff --git a/UploadInterface.php b/UploadInterface.php
new file mode 100644
index 0000000..fad5048
--- /dev/null
+++ b/UploadInterface.php
@@ -0,0 +1,23 @@
+