Merge pull request #6500 from marc1706/ticket/15687

[ticket/15687] Add attachment filename to attachment URL
This commit is contained in:
Marc Alexander 2023-07-26 15:19:36 +02:00 committed by GitHub
commit 1712ee610a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 78 additions and 17 deletions

View file

@ -4,8 +4,9 @@ phpbb_storage_avatar:
_controller: storage.controller.avatar:handle _controller: storage.controller.avatar:handle
phpbb_storage_attachment: phpbb_storage_attachment:
path: /attachment/{file} path: /attachment/{id}/{filename}
defaults: defaults:
_controller: storage.controller.attachment:handle filename: ''
_controller: storage.controller.attachment:handle_attachment
requirements: requirements:
id: \d+ id: \d+

View file

@ -47,7 +47,7 @@ $thumbnail = $request->variable('t', false);
$response = new RedirectResponse( $response = new RedirectResponse(
$controller_helper->route('phpbb_storage_attachment', array( $controller_helper->route('phpbb_storage_attachment', array(
'file' => $attach_id, 'id' => $attach_id,
't' => $thumbnail, 't' => $thumbnail,
), false), ), false),
301 301

View file

@ -1112,7 +1112,13 @@ class acp_attachments
'PHYSICAL_FILENAME' => utf8_basename($row['physical_filename']), 'PHYSICAL_FILENAME' => utf8_basename($row['physical_filename']),
'ATTACH_ID' => $row['attach_id'], 'ATTACH_ID' => $row['attach_id'],
'POST_ID' => (!empty($post_ids[$row['attach_id']])) ? $post_ids[$row['attach_id']] : '', 'POST_ID' => (!empty($post_ids[$row['attach_id']])) ? $post_ids[$row['attach_id']] : '',
'U_FILE' => $this->controller_helper->route('phpbb_storage_attachment', ['file' => (int) $row['attach_id']]) 'U_FILE' => $this->controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $row['attach_id'],
'filename' => $row['real_filename'],
]
),
]); ]);
} }
$db->sql_freeresult($result); $db->sql_freeresult($result);
@ -1302,7 +1308,13 @@ class acp_attachments
'S_IN_MESSAGE' => (bool) $row['in_message'], 'S_IN_MESSAGE' => (bool) $row['in_message'],
'U_VIEW_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p={$row['post_msg_id']}") . "#p{$row['post_msg_id']}", 'U_VIEW_TOPIC' => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "p={$row['post_msg_id']}") . "#p{$row['post_msg_id']}",
'U_FILE' => $this->controller_helper->route('phpbb_storage_attachment', ['file' => $row['attach_id']]) 'U_FILE' => $this->controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $row['attach_id'],
'filename' => $row['real_filename'],
]
)
)); ));
} }

View file

@ -2306,7 +2306,13 @@ class acp_users
'S_IN_MESSAGE' => $row['in_message'], 'S_IN_MESSAGE' => $row['in_message'],
'U_DOWNLOAD' => $controller_helper->route('phpbb_storage_attachment', ['file' => (int) $row['attach_id']]), 'U_DOWNLOAD' => $controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $row['attach_id'],
'filename' => $row['real_filename'],
]
),
'U_VIEW_TOPIC' => $view_topic) 'U_VIEW_TOPIC' => $view_topic)
); );
} }

View file

@ -1285,14 +1285,26 @@ function parse_attachments($forum_id, &$message, &$attachments, &$update_count_a
$display_cat = attachment_category::NONE; $display_cat = attachment_category::NONE;
} }
$download_link = $controller_helper->route('phpbb_storage_attachment', ['file' => (int) $attachment['attach_id']]); $download_link = $controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $attachment['attach_id'],
'filename' => $attachment['real_filename'],
]
);
$l_downloaded_viewed = 'VIEWED_COUNTS'; $l_downloaded_viewed = 'VIEWED_COUNTS';
switch ($display_cat) switch ($display_cat)
{ {
// Images // Images
case attachment_category::IMAGE: case attachment_category::IMAGE:
$inline_link = $controller_helper->route('phpbb_storage_attachment', ['file' => (int) $attachment['attach_id']]); $inline_link = $controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $attachment['attach_id'],
'filename' => $attachment['real_filename'],
]
);
$block_array += array( $block_array += array(
'S_IMAGE' => true, 'S_IMAGE' => true,
@ -1304,7 +1316,14 @@ function parse_attachments($forum_id, &$message, &$attachments, &$update_count_a
// Images, but display Thumbnail // Images, but display Thumbnail
case attachment_category::THUMB: case attachment_category::THUMB:
$thumbnail_link = $controller_helper->route('phpbb_storage_attachment', ['file' => (int) $attachment['attach_id'], 't' => 1]); $thumbnail_link = $controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $attachment['attach_id'],
'filename' => $attachment['real_filename'],
't' => 1,
]
);
$block_array += array( $block_array += array(
'S_THUMBNAIL' => true, 'S_THUMBNAIL' => true,

View file

@ -868,7 +868,14 @@ function posting_gen_attachment_entry($attachment_data, &$filename_data, $show_a
$hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />'; $hidden .= '<input type="hidden" name="attachment_data[' . $count . '][' . $key . ']" value="' . $value . '" />';
} }
$download_link = $phpbb_container->get('controller.helper')->route('phpbb_storage_attachment', ['file' => (int) $attach_row['attach_id']]); $download_link = $phpbb_container->get('controller.helper')
->route(
'phpbb_storage_attachment',
[
'id' => (int) $attach_row['attach_id'],
'filename' => $attach_row['real_filename'],
]
);
$attachrow_template_vars[(int) $attach_row['attach_id']] = array( $attachrow_template_vars[(int) $attach_row['attach_id']] = array(
'FILENAME' => utf8_basename($attach_row['real_filename']), 'FILENAME' => utf8_basename($attach_row['real_filename']),

View file

@ -1716,7 +1716,13 @@ class parse_message extends bbcode_firstpass
if (isset($this->plupload) && $this->plupload->is_active()) if (isset($this->plupload) && $this->plupload->is_active())
{ {
$download_url = $controller_helper->route('phpbb_storage_attachment', ['file' => (int) $new_entry['attach_id']]); $download_url = $controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $new_entry['attach_id'],
'filename' => $new_entry['real_filename'],
]
);
// Send the client the attachment data to maintain state // Send the client the attachment data to maintain state
$json_response->send(array('data' => $this->attachment_data, 'download_url' => $download_url)); $json_response->send(array('data' => $this->attachment_data, 'download_url' => $download_url));

View file

@ -185,7 +185,13 @@ class ucp_attachments
'S_IN_MESSAGE' => $row['in_message'], 'S_IN_MESSAGE' => $row['in_message'],
'S_LOCKED' => !$row['in_message'] && !$auth->acl_get('m_edit', $row['forum_id']) && ($row['forum_status'] == ITEM_LOCKED || $row['topic_status'] == ITEM_LOCKED || $row['post_edit_locked']), 'S_LOCKED' => !$row['in_message'] && !$auth->acl_get('m_edit', $row['forum_id']) && ($row['forum_status'] == ITEM_LOCKED || $row['topic_status'] == ITEM_LOCKED || $row['post_edit_locked']),
'U_VIEW_ATTACHMENT' => $controller_helper->route('phpbb_storage_attachment', ['file' => (int) $row['attach_id']]), 'U_VIEW_ATTACHMENT' => $controller_helper->route(
'phpbb_storage_attachment',
[
'id' => (int) $row['attach_id'],
'filename' => $row['real_filename'],
]
),
'U_VIEW_TOPIC' => $view_topic) 'U_VIEW_TOPIC' => $view_topic)
); );

View file

@ -86,11 +86,14 @@ class attachment extends controller
} }
/** /**
* {@inheritdoc} * Handle attachments
*
* @param int $id File ID
* @param string $filename Filename
*/ */
public function handle(string $file): Response public function handle_attachment(int $id, string $filename): Response
{ {
$attach_id = (int) $file; $attach_id = $id;
$thumbnail = $this->request->variable('t', false); $thumbnail = $this->request->variable('t', false);
$this->language->add_lang('viewtopic'); $this->language->add_lang('viewtopic');
@ -109,7 +112,8 @@ class attachment extends controller
is_orphan, physical_filename, real_filename, extension, mimetype, is_orphan, physical_filename, real_filename, extension, mimetype,
filesize, filetime filesize, filetime
FROM ' . ATTACHMENTS_TABLE . " FROM ' . ATTACHMENTS_TABLE . "
WHERE attach_id = $attach_id"; WHERE attach_id = $attach_id" .
(($filename) ? " AND real_filename = '" . $this->db->sql_escape($filename) . "'" : '');
$result = $this->db->sql_query($sql); $result = $this->db->sql_query($sql);
$attachment = $this->db->sql_fetchrow($result); $attachment = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result); $this->db->sql_freeresult($result);

View file

@ -86,7 +86,7 @@ class phpbb_functional_acp_attachments_test extends phpbb_functional_test_case
// Get attach id, the link looks similar to ./download/attachment/3 // Get attach id, the link looks similar to ./download/attachment/3
$attach_link = $crawler->filter('span.file-name > a')->attr('href'); $attach_link = $crawler->filter('span.file-name > a')->attr('href');
$matches = []; $matches = [];
preg_match('/\/([0-9]+)$/', $attach_link, $matches); preg_match('/\/([0-9]+)\/valid\.jpg$/', $attach_link, $matches);
$attach_id = (int) $matches[1]; $attach_id = (int) $matches[1];
// Set file time older than 3 hours to consider it orphan // Set file time older than 3 hours to consider it orphan