Skip to content

Commit 538f70e

Browse files
authored
Merge pull request #583 from code16/upload-temporary-storage
Allow to have temporary only upload
2 parents 799ec31 + b32c79c commit 538f70e

File tree

7 files changed

+111
-58
lines changed

7 files changed

+111
-58
lines changed

docs/guide/form-fields/upload.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Set the destination base storage path.
3939
If you want to use a `{id}` special placeholder to add the instance id in the path (for instance: `$field->setStorageBasePath('/users/{id}/avatar')`), you must be the Eloquent case, leveraging `Code16\Sharp\Form\Eloquent\WithSharpFormEloquentUpdater` (see [Eloquent form](../building-form#eloquent-case-where-the-magic-happens))
4040
:::
4141

42+
### `setStorageTemporary()`
43+
44+
Keep the file only in the upload directory/disk (configured [here](#general-configuration)). `setStorageDisk()` and `setStorageBasePath()` will be ignored.
45+
4246
### `setAllowedExtensions(string|array $allowedExtensions)`
4347

4448
Define the allowed file extensions.
@@ -246,4 +250,4 @@ class SharpServiceProvider extends SharpAppServiceProvider
246250
}
247251
```
248252

249-
Queue and connection should be [properly configured](https://laravel.com/docs/queues).
253+
Queue and connection should be [properly configured](https://laravel.com/docs/queues).

src/Form/Fields/Editor/Uploads/SharpFormEditorUpload.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Code16\Sharp\Form\Fields\Editor\Uploads;
44

5+
use Code16\Sharp\Exceptions\SharpInvalidConfigException;
56
use Code16\Sharp\Form\Fields\Formatters\UploadFormatter;
67
use Code16\Sharp\Form\Fields\SharpFormUploadField;
78

@@ -13,7 +14,15 @@ public static function make(?string $key = 'file'): self
1314
{
1415
return new static($key, 'upload', app(UploadFormatter::class));
1516
}
16-
17+
18+
/**
19+
* @throws SharpInvalidConfigException
20+
*/
21+
public function setStorageTemporary(): SharpFormUploadField
22+
{
23+
throw new SharpInvalidConfigException('Temporary storage is not available for editor uploads');
24+
}
25+
1726
public function setHasLegend(bool $hasLegend = true): self
1827
{
1928
$this->hasLegend = $hasLegend;

src/Form/Fields/Formatters/UploadFormatter.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,35 @@ public function fromFront(SharpFormField $field, string $attribute, $value): ?ar
3131
);
3232

3333
return tap($this->normalizeFromFront($value, [
34-
'file_name' => sprintf(
35-
'%s/%s',
36-
str($field->storageBasePath())->replace('{id}', $this->instanceId ?? '{id}'),
37-
app(FileUtil::class)->findAvailableName(
38-
$value['name'], $field->storageBasePath(), $field->storageDisk(),
34+
'file_name' => $field->storageDisk()
35+
? sprintf(
36+
'%s/%s',
37+
str($field->storageBasePath())->replace('{id}', $this->instanceId ?? '{id}'),
38+
app(FileUtil::class)->findAvailableName(
39+
$value['name'], $field->storageBasePath(), $field->storageDisk(),
40+
)
3941
)
40-
),
42+
: $uploadedFieldRelativePath,
4143
'size' => Storage::disk(sharp()->config()->get('uploads.tmp_disk'))
4244
->size($uploadedFieldRelativePath),
4345
'mime_type' => Storage::disk(sharp()->config()->get('uploads.tmp_disk'))
4446
->mimeType($uploadedFieldRelativePath),
45-
'disk' => $field->storageDisk(),
47+
'disk' => $field->storageDisk() ?: sharp()->config()->get('uploads.tmp_disk'),
4648
'filters' => $field->isImageTransformOriginal()
4749
? null
4850
: $value['filters'] ?? null,
4951
]), function ($formatted) use ($field, $value) {
50-
app(SharpUploadManager::class)->queueHandleUploadedFile(
51-
uploadedFileName: $value['name'],
52-
disk: $field->storageDisk(),
53-
filePath: $formatted['file_name'],
54-
shouldOptimizeImage: $field->isImageOptimize(),
55-
transformFilters: $field->isImageTransformOriginal()
56-
? ($value['filters'] ?? null)
57-
: null,
58-
);
52+
if($field->storageDisk()) {
53+
app(SharpUploadManager::class)->queueHandleUploadedFile(
54+
uploadedFileName: $value['name'],
55+
disk: $field->storageDisk(),
56+
filePath: $formatted['file_name'],
57+
shouldOptimizeImage: $field->isImageOptimize(),
58+
transformFilters: $field->isImageTransformOriginal()
59+
? ($value['filters'] ?? null)
60+
: null,
61+
);
62+
}
5963
});
6064
}
6165

src/Form/Fields/SharpFormUploadField.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
use Closure;
66
use Code16\Sharp\Form\Fields\Formatters\UploadFormatter;
7-
use Code16\Sharp\Form\Fields\Utils\IsUploadField;
87
use Code16\Sharp\Utils\Fields\Validation\SharpFileValidation;
98
use Code16\Sharp\Utils\Fields\Validation\SharpImageValidation;
109
use Illuminate\Validation\Rules\Dimensions;
1110

12-
class SharpFormUploadField extends SharpFormField implements IsUploadField
11+
class SharpFormUploadField extends SharpFormField
1312
{
1413
const FIELD_TYPE = 'upload';
1514

16-
protected string $storageDisk = 'local';
15+
protected ?string $storageDisk = 'local';
1716
protected string|Closure $storageBasePath = 'data';
1817
protected ?float $maxFileSize = null;
1918
protected ?float $minFileSize = null;
@@ -138,6 +137,13 @@ public function setStorageDisk(string $storageDisk): self
138137

139138
return $this;
140139
}
140+
141+
public function setStorageTemporary(): self
142+
{
143+
$this->storageDisk = null;
144+
145+
return $this;
146+
}
141147

142148
/**
143149
* @param string|(Closure(): string) $storageBasePath
@@ -157,7 +163,7 @@ public function setAllowedExtensions(string|array $extensions): self
157163
return $this;
158164
}
159165

160-
public function storageDisk(): string
166+
public function storageDisk(): ?string
161167
{
162168
return $this->storageDisk;
163169
}

src/Form/Fields/Utils/IsUploadField.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

tests/Http/Form/HandlesUploadedFilesInRequestTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,48 @@ public function buildFormFields(FieldsContainer $formFields): void
282282
Queue::assertNotPushed(HandleUploadedFileJob::class);
283283
});
284284

285+
it('does not dispatch HandlePostedFilesJob when temporary', function () {
286+
fakeFormFor('person', new class() extends PersonForm
287+
{
288+
public function buildFormFields(FieldsContainer $formFields): void
289+
{
290+
$formFields
291+
->addField(
292+
SharpFormUploadField::make('file')
293+
->setStorageTemporary()
294+
);
295+
}
296+
});
297+
298+
UploadedFile::fake()
299+
->image('image.jpg')
300+
->storeAs('/tmp', 'image.jpg', ['disk' => 'local']);
301+
302+
$this
303+
->post('/sharp/s-list/person/s-form/person/2', [
304+
'name' => 'Stephen Hawking',
305+
'file' => [
306+
'name' => 'image.jpg',
307+
'uploaded' => true,
308+
],
309+
])
310+
->assertSessionHasNoErrors()
311+
->assertRedirect();
312+
313+
$this
314+
->post('/sharp/s-list/person/s-form/person', [
315+
'name' => 'Marie Curie',
316+
'file' => [
317+
'name' => 'image.jpg',
318+
'uploaded' => true,
319+
],
320+
])
321+
->assertSessionHasNoErrors()
322+
->assertRedirect();
323+
324+
Queue::assertNotPushed(HandleUploadedFileJob::class);
325+
});
326+
285327
it('handles isTransformOriginal to transform the image on a newly uploaded file', function ($transformKeepOriginal) {
286328
fakeFormFor('person', new class($transformKeepOriginal) extends PersonForm
287329
{

tests/Unit/Form/Fields/Formatters/UploadFormatterTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,27 @@
122122
],
123123
]);
124124
});
125+
126+
it('format temporary upload from front', function () {
127+
$formatter = app(UploadFormatter::class);
128+
129+
UploadedFile::fake()
130+
->image('image.jpg')
131+
->storeAs('/tmp', 'image.jpg', ['disk' => 'local']);
132+
133+
$field = SharpFormUploadField::make('upload')->setStorageTemporary();
134+
135+
expect(
136+
$formatter
137+
->fromFront($field, 'attr', [
138+
'name' => 'image.jpg',
139+
'uploaded' => true,
140+
])
141+
)
142+
->toEqual([
143+
'file_name' => 'tmp/image.jpg',
144+
'disk' => 'local',
145+
'mime_type' => 'image/jpeg',
146+
'size' => 695,
147+
]);
148+
});

0 commit comments

Comments
 (0)