Skip to content

Commit dd97efb

Browse files
imorlandStyleCIBot
andauthored
fix: bidirectional compatibility for aws-s3/awss3 adapter names (FriendsOfFlarum#454)
* fix: adpator mismatch when forcing to aws-s3 * Apply fixes from StyleCI * fix test * actually test something --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent 6a82537 commit dd97efb

5 files changed

Lines changed: 682 additions & 4 deletions

File tree

UPGRADE-2.0.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Upgrade Guide for Flarum 2.0
2+
3+
## AWS S3 Adapter Name Changes
4+
5+
### Background
6+
7+
In earlier versions of fof/upload, both `'aws-s3'` and `'awss3'` adapter names were registered, but only `'aws-s3'` had a corresponding method. This caused validation errors when trying to use the `'awss3'` adapter name.
8+
9+
A fix was implemented to provide bidirectional compatibility: both adapter names now work and map to the same `awsS3()` method. This ensures backward compatibility for:
10+
- Old files in the database with `upload_method = 'awss3'` or `upload_method = 'aws-s3'`
11+
- Admin configurations forcing one variant while DB has the other
12+
- MIME type configurations using either variant
13+
14+
### Changes for Flarum 2.0
15+
16+
For Flarum 2.0, we will standardize on `'aws-s3'` as the canonical adapter name and remove support for `'awss3'`.
17+
18+
### Migration Steps
19+
20+
#### 1. Database Migration
21+
22+
Create a migration to update all existing records:
23+
24+
```php
25+
<?php
26+
27+
use Illuminate\Database\Schema\Builder;
28+
29+
return [
30+
'up' => function (Builder $schema) {
31+
// Update files table
32+
$schema
33+
->getConnection()
34+
->table('fof_upload_files')
35+
->where('upload_method', 'awss3')
36+
->update(['upload_method' => 'aws-s3']);
37+
38+
// Update MIME type settings
39+
$mimeConfiguration = $schema
40+
->getConnection()
41+
->table('settings')
42+
->where('key', 'fof-upload.mimeTypes')
43+
->value('value');
44+
45+
if ($mimeConfiguration) {
46+
$mimeConfiguration = json_decode($mimeConfiguration, true);
47+
48+
foreach ($mimeConfiguration as $mime => &$config) {
49+
if (isset($config['adapter']) && $config['adapter'] === 'awss3') {
50+
$config['adapter'] = 'aws-s3';
51+
}
52+
}
53+
54+
$schema
55+
->getConnection()
56+
->table('settings')
57+
->where('key', 'fof-upload.mimeTypes')
58+
->update(['value' => json_encode($mimeConfiguration)]);
59+
}
60+
},
61+
];
62+
```
63+
64+
#### 2. Code Changes
65+
66+
Search for `TODO: Flarum 2.0` in the codebase and make the following changes:
67+
68+
**src/Adapters/Manager.php:**
69+
- Line 55: Remove `'awss3' => class_exists(S3Client::class),`
70+
- Lines 76-83: Remove the bidirectional compatibility check block
71+
- Lines 89-92: Remove the normalization line, use `$adapter` directly
72+
73+
**resources/locale/en.yml:**
74+
- Line 144: Remove `awss3: AWS S3` translation entry
75+
76+
**Tests to Remove:**
77+
- `tests/unit/Adapters/ManagerTest.php`:
78+
- Line 72: Remove `'awss3'` assertion
79+
- Lines 102-138: Remove `instantiate_normalizes_awss3_to_aws_s3` test
80+
81+
- `tests/integration/api/AdaptersExtenderTest.php`:
82+
- Lines 28-49: Remove `force_extender_limits_available_adapters_to_awss3` test
83+
- Lines 51-84: Remove `force_extender_allows_instantiation_of_forced_awss3_adapter` test
84+
- Line 177: Remove `'awss3'` assertion
85+
- Lines 186-228: Remove `force_aws_s3_when_db_has_awss3_configuration` test
86+
- Lines 230-272: Remove `force_awss3_when_db_has_aws_s3_configuration` test
87+
- Lines 274-311: Remove `awss3_instantiation_works_with_normalization` test
88+
89+
#### 3. Update Documentation
90+
91+
Update any user-facing documentation that references the `'awss3'` adapter name to use `'aws-s3'` instead.
92+
93+
#### 4. Breaking Change Notice
94+
95+
Add a breaking change notice in the changelog:
96+
97+
```markdown
98+
### Breaking Changes
99+
100+
- **AWS S3 Adapter:** The legacy `'awss3'` adapter name has been removed. The canonical name is now `'aws-s3'`.
101+
- If you were using `->force('awss3')` in your `extend.php`, change it to `->force('aws-s3')`
102+
- Database records are automatically migrated during upgrade
103+
```
104+
105+
### Testing
106+
107+
After applying these changes:
108+
109+
1. Run all unit tests: `composer test:unit`
110+
2. Run all integration tests: `composer test:integration`
111+
3. Verify that:
112+
- Files with `upload_method = 'aws-s3'` can be rendered
113+
- New uploads use `'aws-s3'` as the adapter name
114+
- Forcing `'aws-s3'` works correctly
115+
- Old references to `'awss3'` no longer exist in the codebase
116+
117+
### Support Period
118+
119+
The bidirectional compatibility (`'awss3'``'aws-s3'`) will be maintained until Flarum 2.0 is released. This gives administrators time to:
120+
- Update their `extend.php` configurations
121+
- Run the migration
122+
- Test their installations

resources/locale/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ fof-upload:
140140
Inserts a preview (first 5 lines) of the text file, with an option to expand to reveal the full contents of the file.
141141
upload_methods:
142142
aws-s3: S3 or Compatible
143+
# TODO: Flarum 2.0 - Remove 'awss3' backward compatibility translation
143144
awss3: AWS S3
144145
imgur: Imgur
145146
local: Local

src/Adapters/Manager.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function adapters(): Collection
4848
{
4949
$adapters = Collection::make([
5050
'aws-s3' => class_exists(S3Client::class),
51+
// TODO: Flarum 2.0 - Remove 'awss3' backward compatibility entry.
52+
// This was added to fix a bug where 'awss3' was registered but had no method.
53+
// Both 'aws-s3' and 'awss3' now map to the same awsS3() method.
54+
// For 2.0: Remove this line and update migrations to convert all 'awss3' to 'aws-s3'.
5155
'awss3' => class_exists(S3Client::class),
5256
'imgur' => true,
5357
'qiniu' => class_exists(QiniuClient::class),
@@ -61,18 +65,32 @@ public function adapters(): Collection
6165

6266
public function instantiate(string $adapter): UploadAdapter
6367
{
64-
$configured = $this->adapters()
68+
$adapters = $this->adapters()
6569
// Drops adapters that cannot be instantiated due to missing packages.
6670
->filter(function ($available) {
6771
return $available;
68-
})
69-
->get($adapter);
72+
});
73+
74+
$configured = $adapters->get($adapter);
75+
76+
// TODO: Flarum 2.0 - Remove this backward compatibility check.
77+
// Problem: Old files in database may have upload_method='awss3' while admin forces 'aws-s3', or vice versa.
78+
// Solution: Treat both adapter names as equivalent when one is forced.
79+
// For 2.0: Remove this block after ensuring all databases use 'aws-s3' consistently.
80+
if (!$configured && in_array($adapter, ['aws-s3', 'awss3'])) {
81+
$alternativeAdapter = $adapter === 'aws-s3' ? 'awss3' : 'aws-s3';
82+
$configured = $adapters->get($alternativeAdapter);
83+
}
7084

7185
if (!$configured) {
7286
throw new ValidationException(['upload' => "No adapter configured for $adapter"]);
7387
}
7488

75-
$method = Str::camel($adapter);
89+
// TODO: Flarum 2.0 - Remove this normalization.
90+
// Both 'awss3' and 'aws-s3' map to awsS3() method because Str::camel('awss3') doesn't add capital S.
91+
// For 2.0: Remove this line when 'awss3' is no longer supported.
92+
$normalizedAdapter = $adapter === 'awss3' ? 'aws-s3' : $adapter;
93+
$method = Str::camel($normalizedAdapter);
7694

7795
$driver = $this->events->until(new Instantiate($adapter, $this->util));
7896

0 commit comments

Comments
 (0)