October: Choose filename of uploaded images /files on disk for SEO

Created on 25 Aug 2016  Â·  14Comments  Â·  Source: octobercms/october

I'm trying to save Uploaded images with a filename based on their respective attached models, so for example an image is used for a track which has a genre so I would like the link to the image to be pathToFile/Genre-TrackName.jpg for google seo.

Now the attachment/File model automatically generates a unique id for the filename and puts it in a three layer folder based on the unique key. Is there a reason for this with respect to disk IO speed optimization?

If I would set the disk_name of the file to what I want it to be before it is saved does that mess up the potential IO speed? Or should I extend the filebase and also overload the getPartitionDirectory function and use a different table than system_files.

I bet this could also be done by just moving the file in the afterSave() method, but that feels like I'm wasting server resources in not storing it in the right place right away.

Question

Most helpful comment

Alt solution found

<a href="{{ data.file.getPath() }}" download="{{ data.file.getFileName() }}" class="download-link">

All 14 comments

The GitHub issue list is for reporting bugs in the OctoberCMS code base specifically. Please try the following support avenues for getting support with using October:

This has come up before. You could solve it by using your own file model (extending System\Models\File). Otherwise the media manager produces nice SEO names. You could also use a rewrite to make the current system files nicer.

Alt solution found

<a href="{{ data.file.getPath() }}" download="{{ data.file.getFileName() }}" class="download-link">

Hi Samuel,

Thanks for still looking in to this.
I fixed this back then by extending the file model so that it converts
filenames to SEO friendly links with the filename and thumb encoded in the
path. These links then get redirected via .htaccess in apache to the
original octobercms filesystem logic so there is no performance loss.

Doesn't change the actual saving of the files though.

Cheers,
Jaap

On Sep 8, 2017 4:40 AM, "Samuel Georges" notifications@github.com wrote:

Alt solution found

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/octobercms/october/issues/2330#issuecomment-327983536,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AIhgU007JcNNDP6djr6YVCF-i7ojzWtGks5sgKkwgaJpZM4Js8ef
.

@GitJaap Can you share the code please ? :-)

@GitJaap Could you please share the code ?
@shiftaltd Did you find any other solutions ?

@GitJaap Same question than @AlexisWalravens
@shiftaltd Same question than @AlexisWalravens
@AlexisWalravens Same question than you

:)

@GitJaap how did you solve this issue?

The PHP code would look something like this:

<?php
class SeoFriendlyFile extends \System\Models\File
{
    public function getPath($fileName = null)
    {
        return parent::getPath($fileName) . '/' . $this->file_name;
     }
}

which would turn storage/app/uploads/public/123/qwe/1d2/123qwe1d2asdlfkasdf2.jpg into storage/app/uploads/public/123/qwe/1d2/123qwe1d2asdlfkasdf2.jpg/nice_seo_filename.jpg, which you would then use .htaccess rules to redirect to storage/app/uploads/public/123/qwe/1d2/123qwe1d2asdlfkasdf2.jpg by stripping off the last part of storage/app/uploads/public/{segment1}/{segment2}/{segment3}/{filename}/{removethis}

@LukeTowers @GitJaap would you mind sharing the .htaccess redirect rules please? Thanks!

@juanmoyano I never actually wrote any, that was just an example. Perhaps @bennothommo or @seanthepottingshed could help?

Sorry we use nginx so I have no knowledge of the dark arts of this .htaccess you speak of

@juanmoyano Here's an example of the rewrite rules, based off @LukeTowers suggestion:

SeoFile.php

<?php
class SeoFile extends \System\Models\File
{
    public function getPath($fileName = null)
    {
        $parts = pathinfo(parent::getPath($fileName));
        return $parts['dirname'] . '/' . $parts['filename'] . '/' . $this->file_name;
    }
}

Turns, for example, storage/app/uploads/public/5f1/6a0/b9b/thumb_12_200_200_0_0_crop.jpg into storage/app/uploads/public/5f1/6a0/b9b/thumb_12_200_200_0_0_crop/foto26df966.jpg. Then...

.htaccess

##
## SEO file rewrite
##
RewriteCond %{DOCUMENT_ROOT}/storage/app/uploads/$1.$3 -f
RewriteRule ^storage/app/uploads/(.+)/(.+?)\.([a-z0-9]+)$ /storage/app/uploads/$1.$3 [L,NC]

I'm not responsible for this particular code suggestion ruining your work week.

@bennothommo Thanks! I'll give it a shot!

Was this page helpful?
0 / 5 - 0 ratings