Skip to content

Commit

Permalink
chore: refactor cli command, drop regions, introduce cache header
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 23, 2024
1 parent de1e181 commit 0bad82b
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 167 deletions.
62 changes: 59 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The S3 (or compatible) bucket can be configured either by environment variables
#### Environment variables
- `FOF_S3_ACCESS_KEY_ID` - your access key ID *
- `FOF_S3_SECRET_ACCESS_KEY` - your secret *
- `FOF_S3_DEFAULT_REGION` - the region *
- `FOF_S3_REGION` - the region *
- `FOF_S3_BUCKET` - the bucket name *
- `FOF_S3_URL` - the public facing base URL of the bucket
- `FOF_S3_ENDPOINT` - the ARN
Expand All @@ -39,12 +39,68 @@ If you plan to setup the S3 configuration using the environment variables, pleas

After your new bucket is configured, any exisiting files, will not exist there (ie uploaded avatars, profile covers, etc).

Use the provided command to start moving these files:
Use the provided command to start copying these files. An optional additional paramater `--move` will delete the files from your local filesystem after a successful copy.

```php
php flarum fof:s3:move
php flarum fof:s3:copy --move
```

## Links

- [Discuss](https://discuss.flarum.org/d/PUT_DISCUSS_SLUG_HERE)


[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"POST",
"GET",
"PUT"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": [],
"MaxAgeSeconds": 0
}
]


{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::nothing-forum-test/*"
}
]
}


{
"Version": "2012-10-17",
"Id": "http referer policy",
"Statement": [
{
"Sid": "Allow get requests originating from www.nothing.community and nothing.community.",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject",
"s3:GetObjectVersion"
],
"Resource": "arn:aws:s3:::nothing-forum-test/*",
"Condition": {
"StringLike": {
"aws:Referer": "https://fstlnforum.bbxlk.cc*"
}
}
}
]

}
2 changes: 1 addition & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
->register(Provider\S3DiskProvider::class),

(new Extend\Console())
->command(Console\MoveAssetsCommand::class),
->command(Console\CopyAssetsCommand::class),

(new Extend\Filesystem())
->driver('s3', Driver\S3Driver::class)
Expand Down
13 changes: 3 additions & 10 deletions js/src/admin/components/S3SettingsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default class S3SettingsPage extends ExtensionPage {
'awsS3Key',
this.buildSettingComponent({
setting: `${this.settingPrefix}awsS3Key`,
type: 'password',
type: 'string',
label: app.translator.trans('fof-s3-assets.admin.settings.s3key.label'),
help: app.translator.trans('fof-s3-assets.admin.settings.s3key.help'),
})
Expand All @@ -106,7 +106,7 @@ export default class S3SettingsPage extends ExtensionPage {
'awsS3Secret',
this.buildSettingComponent({
setting: `${this.settingPrefix}awsS3Secret`,
type: 'password',
type: 'string',
label: app.translator.trans('fof-s3-assets.admin.settings.s3secret.label'),
help: app.translator.trans('fof-s3-assets.admin.settings.s3secret.help'),
})
Expand All @@ -116,14 +116,7 @@ export default class S3SettingsPage extends ExtensionPage {
'awsS3Region',
this.buildSettingComponent({
setting: `${this.settingPrefix}awsS3Region`,
type: 'select',
options: this.awsRegions.reduce(
(options, region) => {
options[region.value] = `${region.label} (${region.value})`;
return options;
},
{} as Record<string, string>
),
type: 'string',
label: app.translator.trans('fof-s3-assets.admin.settings.s3region.label'),
help: app.translator.trans('fof-s3-assets.admin.settings.s3region.help'),
})
Expand Down
80 changes: 80 additions & 0 deletions src/Console/CopyAssetsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of fof/s3-assets.
*
* Copyright (c) FriendsOfFlarum
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\S3Assets\Console;

use Flarum\Foundation\Console\AssetsPublishCommand;
use Flarum\Foundation\Paths;
use Illuminate\Console\Command;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Contracts\Filesystem\Factory;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;

class CopyAssetsCommand extends Command
{
protected $signature = 'fof:s3:copy
{--move : Delete local files after moving to the S3 disk}';

protected $description = 'Copy assets to the S3 disk';

public function handle(Container $container, Factory $factory, Paths $paths, AssetsPublishCommand $publishCommand)
{
/** @var Filesystem $localFilesystem */
$localFilesystem = $container->make('files');

// Determine if files should be deleted after moving
$deleteAfterMove = $this->option('move');

// Move assets
$this->info($deleteAfterMove ? 'Moving assets...' : 'Copying assets...');
$this->moveFilesToDisk($localFilesystem, $paths->public . '/assets', $factory->disk('flarum-assets'), $deleteAfterMove);

$publishCommand->run(
new ArrayInput([]),
new ConsoleOutput()
);
}

/**
* Get the registered disks.
*
* @return array
*/
protected function getFlarumDisks(): array
{
return resolve('flarum.filesystem.disks');
}

protected function moveFilesToDisk(Filesystem $localFilesystem, string $localPath, Cloud $disk, bool $deleteAfterMove): void
{
$count = count($localFilesystem->allFiles($localPath));
$this->output->progressStart($count);

foreach ($localFilesystem->allFiles($localPath) as $file) {
/** @var \Symfony\Component\Finder\SplFileInfo $file */
$written = $disk->put($file->getRelativePathname(), $file->getContents());

if ($written) {
if ($deleteAfterMove) {
$localFilesystem->delete($file->getPathname());
}
$this->output->progressAdvance();
} else {
throw new \Exception('File did not copy');
}
}

$this->output->progressFinish();
}
}
102 changes: 0 additions & 102 deletions src/Console/MoveAssetsCommand.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Content/AdminPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public function __construct(
public function __invoke(Document $document)
{
$document->payload['s3SetByEnv'] = $this->s3Config->shouldUseEnv();
$document->payload['FoFS3Regions'] = $this->s3->getAwsRegions();
$document->payload['FoFS3ShareWithFoFUpload'] = $this->settings->get('fof-s3-assets.share_s3_config_with_fof_upload');
}
}
11 changes: 7 additions & 4 deletions src/Driver/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function config(): array
return $config;
}

protected function buildConfigArray(string $key, string $secret, string $region, string $bucket, string $cdnUrl, ?string $endpoint, ?bool $pathStyle, ?string $acl, bool $setByEnv = false, string $driver = 's3'): array
protected function buildConfigArray(string $key, string $secret, string $region, string $bucket, string $cdnUrl, ?string $endpoint, ?bool $pathStyle, ?string $acl, bool $setByEnv = false, string $cache = 'max-age=31536000', string $driver = 's3'): array
{
// These are the required values for AWS S3.
// Some S3-compatible services may require additional values, so we check if any of these are set below.
Expand All @@ -64,6 +64,7 @@ protected function buildConfigArray(string $key, string $secret, string $region,
'bucket' => $bucket,
'url' => $cdnUrl,
'set_by_environment' => $setByEnv,
'options' => [],
];

// These values are generally only required for S3-compatible services.
Expand All @@ -77,9 +78,11 @@ protected function buildConfigArray(string $key, string $secret, string $region,
}

if ($acl) {
$config['options'] = [
'ACL' => $acl,
];
$config['options']['ACL'] = $acl;
}

if ($cache) {
$config['options']['CacheControl'] = $cache;
}

return $config;
Expand Down
9 changes: 8 additions & 1 deletion src/Driver/S3Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,19 @@ public function build(
return $this->manager->createLocalDriver($localConfig);
}

$start = microtime(true);

$root = Arr::get($localConfig, 'root');
$root = str_replace($this->paths->public, '', $root);

return $this->manager->createS3Driver(array_merge(
$driver = $this->manager->createS3Driver(array_merge(
$this->config->config(),
['root' => $root]
));

$end = microtime(true);
resolve('log')->info("S3 driver built in " . round($end - $start, 2) . "s");

return $driver;
}
}
Loading

0 comments on commit 0bad82b

Please sign in to comment.