Asset: cleaner handling of conflicting filenames
This commit is contained in:
parent
3ed84eb4d5
commit
244af88a9a
@ -33,14 +33,10 @@ class UploadMedia extends HTMLController
|
|||||||
if (empty($uploaded_file))
|
if (empty($uploaded_file))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// DIY slug club.
|
|
||||||
$slug = $tag->slug . '/' . strtr($uploaded_file['name'], [' ' => '-', '--' => '-', '&' => 'and', '=>' => '', "'" => "", ":"=> "", '\\' => '-']);
|
|
||||||
|
|
||||||
$asset = Asset::createNew([
|
$asset = Asset::createNew([
|
||||||
'filename_to_copy' => $uploaded_file['tmp_name'],
|
'filename_to_copy' => $uploaded_file['tmp_name'],
|
||||||
'preferred_filename' => $uploaded_file['name'],
|
'preferred_filename' => $uploaded_file['name'],
|
||||||
'preferred_subdir' => $tag->slug,
|
'preferred_subdir' => $tag->slug,
|
||||||
'slug' => $slug,
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$new_ids[] = $asset->getId();
|
$new_ids[] = $asset->getId();
|
||||||
|
@ -187,9 +187,10 @@ class Asset
|
|||||||
|
|
||||||
$new_filename = $preferred_filename;
|
$new_filename = $preferred_filename;
|
||||||
$destination = ASSETSDIR . '/' . $preferred_subdir . '/' . $preferred_filename;
|
$destination = ASSETSDIR . '/' . $preferred_subdir . '/' . $preferred_filename;
|
||||||
while (file_exists($destination))
|
for ($i = 1; file_exists($destination); $i++)
|
||||||
{
|
{
|
||||||
$filename = pathinfo($preferred_filename, PATHINFO_FILENAME) . '_' . mt_rand(10, 99);
|
$suffix = $i;
|
||||||
|
$filename = pathinfo($preferred_filename, PATHINFO_FILENAME) . ' (' . $suffix . ')';
|
||||||
$extension = pathinfo($preferred_filename, PATHINFO_EXTENSION);
|
$extension = pathinfo($preferred_filename, PATHINFO_EXTENSION);
|
||||||
$new_filename = $filename . '.' . $extension;
|
$new_filename = $filename . '.' . $extension;
|
||||||
$destination = dirname($destination) . '/' . $new_filename;
|
$destination = dirname($destination) . '/' . $new_filename;
|
||||||
@ -206,11 +207,14 @@ class Asset
|
|||||||
$mimetype = finfo_file($finfo, $destination);
|
$mimetype = finfo_file($finfo, $destination);
|
||||||
finfo_close($finfo);
|
finfo_close($finfo);
|
||||||
|
|
||||||
|
// We're going to need the base name a few times...
|
||||||
|
$basename = pathinfo($new_filename, PATHINFO_FILENAME);
|
||||||
|
|
||||||
// Do we have a title yet? Otherwise, use the filename.
|
// Do we have a title yet? Otherwise, use the filename.
|
||||||
$title = isset($data['title']) ? $data['title'] : pathinfo($preferred_filename, PATHINFO_FILENAME);
|
$title = $data['title'] ?? $basename;
|
||||||
|
|
||||||
// Same with the slug.
|
// Same with the slug.
|
||||||
$slug = isset($data['slug']) ? $data['slug'] : $preferred_subdir . '/' . pathinfo($preferred_filename, PATHINFO_FILENAME);
|
$slug = $data['slug'] ?? sprintf('%s/%s', $preferred_subdir, $basename);
|
||||||
|
|
||||||
// Detected an image?
|
// Detected an image?
|
||||||
if (substr($mimetype, 0, 5) == 'image')
|
if (substr($mimetype, 0, 5) == 'image')
|
||||||
|
Loading…
Reference in New Issue
Block a user