Skip to content

Commit 55d074f

Browse files
committed
Attachment API: Fixed error when name not provided in update
Fixes BookStackApp#5353
1 parent 7e6f6af commit 55d074f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

app/Uploads/AttachmentService.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,18 @@ public function updateFileOrderWithinPage(array $attachmentOrder, string $pageId
116116
*/
117117
public function updateFile(Attachment $attachment, array $requestData): Attachment
118118
{
119-
$attachment->name = $requestData['name'];
120-
$link = trim($requestData['link'] ?? '');
119+
if (isset($requestData['name'])) {
120+
$attachment->name = $requestData['name'];
121+
}
121122

123+
$link = trim($requestData['link'] ?? '');
122124
if (!empty($link)) {
123125
if (!$attachment->external) {
124126
$this->deleteFileInStorage($attachment);
125127
$attachment->external = true;
126128
$attachment->extension = '';
127129
}
128-
$attachment->path = $requestData['link'];
130+
$attachment->path = $link;
129131
}
130132

131133
$attachment->save();

tests/Api/AttachmentsApiTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AttachmentsApiTest extends TestCase
1212
{
1313
use TestsApi;
1414

15-
protected $baseEndpoint = '/api/attachments';
15+
protected string $baseEndpoint = '/api/attachments';
1616

1717
public function test_index_endpoint_returns_expected_book()
1818
{
@@ -302,6 +302,23 @@ public function test_update_link_attachment_to_file()
302302
}
303303

304304
public function test_update_file_attachment_to_link()
305+
{
306+
$this->actingAsApiAdmin();
307+
$page = $this->entities->page();
308+
$attachment = $this->createAttachmentForPage($page);
309+
310+
$resp = $this->putJson("{$this->baseEndpoint}/{$attachment->id}", [
311+
'link' => 'https://example.com/donkey',
312+
]);
313+
314+
$resp->assertStatus(200);
315+
$this->assertDatabaseHas('attachments', [
316+
'id' => $attachment->id,
317+
'path' => 'https://example.com/donkey',
318+
]);
319+
}
320+
321+
public function test_update_does_not_require_name()
305322
{
306323
$this->actingAsApiAdmin();
307324
$page = $this->entities->page();

0 commit comments

Comments
 (0)