Skip to content

Commit

Permalink
Initial 3.0 Component
Browse files Browse the repository at this point in the history
  • Loading branch information
zds-s committed Oct 7, 2024
0 parents commit 366c52a
Show file tree
Hide file tree
Showing 14 changed files with 445 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/tests export-ignore
/.github export-ignore
15 changes: 15 additions & 0 deletions .github/workflows/close-pull-request.yml
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! "
25 changes: 25 additions & 0 deletions .github/workflows/release.yml
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
composer.lock
.idea
vendor
*.cache
runtime
44 changes: 44 additions & 0 deletions Event/UploadEvent.php
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;
}
}
27 changes: 27 additions & 0 deletions Event/UploadedEvent.php
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;
}
}
28 changes: 28 additions & 0 deletions Exception/UploadFailException.php
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;
}
}
34 changes: 34 additions & 0 deletions Factory.php
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);
}
}
21 changes: 21 additions & 0 deletions LICENSE
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.
72 changes: 72 additions & 0 deletions Listener/UploadListener.php
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();
}
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# MineAdmin

[Documentation](https://doc.mineadmin.com)
Loading

0 comments on commit 366c52a

Please sign in to comment.