-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 366c52a
Showing
14 changed files
with
445 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/tests export-ignore | ||
/.github export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.<br><br> This Pull Request will close automatically.<br><br> Thanks! " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
composer.lock | ||
.idea | ||
vendor | ||
*.cache | ||
runtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\Upload\Event; | ||
|
||
use Mine\Upload\Upload; | ||
|
||
final class UploadEvent | ||
{ | ||
private ?Upload $upload = null; | ||
|
||
public function __construct( | ||
private readonly \SplFileInfo $uploadFile, | ||
) {} | ||
|
||
public function getUploadFile(): \SplFileInfo | ||
{ | ||
return $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\Upload\Event; | ||
|
||
use Mine\Upload\Upload; | ||
|
||
final class UploadedEvent | ||
{ | ||
public function __construct( | ||
private readonly Upload $upload | ||
) {} | ||
|
||
public function getUpload(): Upload | ||
{ | ||
return $this->upload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\Upload\Exception; | ||
|
||
use Mine\Upload\Event\UploadEvent; | ||
|
||
class UploadFailException extends \RuntimeException | ||
{ | ||
public function __construct(private readonly UploadEvent $event) | ||
{ | ||
parent::__construct('Upload failed'); | ||
} | ||
|
||
public function getEvent(): UploadEvent | ||
{ | ||
return $this->event; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\Upload; | ||
|
||
use Mine\Upload\Event\UploadEvent; | ||
use Mine\Upload\Exception\UploadFailException; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
|
||
final class Factory implements UploadInterface | ||
{ | ||
public function __construct( | ||
private readonly EventDispatcherInterface $dispatcher | ||
) {} | ||
|
||
public function upload(\SplFileInfo $fileInfo): Upload | ||
{ | ||
$event = new UploadEvent($fileInfo); | ||
$this->dispatcher->dispatch($event); | ||
if ($event->isUploaded()) { | ||
return $event->getUpload(); | ||
} | ||
throw new UploadFailException($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of MineAdmin. | ||
* | ||
* @link https://www.mineadmin.com | ||
* @document https://doc.mineadmin.com | ||
* @contact [email protected] | ||
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Mine\Upload\Listener; | ||
|
||
use Hyperf\Event\Contract\ListenerInterface; | ||
use Hyperf\Filesystem\FilesystemFactory; | ||
use Hyperf\Stringable\Str; | ||
use League\Flysystem\Filesystem; | ||
use Mine\Upload\Event\UploadEvent; | ||
use Mine\Upload\Upload; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
abstract class UploadListener implements ListenerInterface | ||
{ | ||
public const ADAPTER_NAME = 'local'; | ||
|
||
private Filesystem $filesystem; | ||
|
||
public function __construct( | ||
FilesystemFactory $filesystemFactory | ||
) { | ||
$this->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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# MineAdmin | ||
|
||
[Documentation](https://doc.mineadmin.com) |
Oops, something went wrong.