Jump to content

Disabling automatic upload renaming


statestreet
 Share

Recommended Posts

On the site for a game development collective I'm part of, I've been trying to add the ability for our developers to attach game builds from Unity (a 3D game engine and development environment) to blog posts. Unity cranks out an HTML file with everything ready to go for static hosting, so I've simply added a file field in the blog post template that accepts the four files Unity exports, one of which is jquery.min.js.

Obviously, this is highly redundant, having the content author upload another copy of jQuery with each build, but it's actually a more streamlined workflow than the alternative of building a standard Unity player template, which would then require the author to create an additional page in ProcessWire each time to attach the Unity file.

So I've gone with this redundant but easy approach. However, I've run into a technical snag: Even after explicitly allowing "min.js" files for the field, the file uploader always renames the jQuery file to "jquery_min.js", which is then not found by the HTML file looking for the original name. Is there a way to disable this behavior?

Link to comment
Share on other sites

Would uploading the whole thing as zip be an option?

Edit:

Just had a look at the source. There is an option to allow dots in filenames (the .min is never parsed as extention), but it's not used by pagefiles. The more fail save way of handling files is still to not allow them.

Link to comment
Share on other sites

I would place one global jquery.min.js at a central place and name it specific to that uploaded files:

  /unity.jquery.min.js

Then I would delete all uploaded jquery.min.js or still would them leave unused.

And in the html file I would correct the url to point to the global one:

$htmlfilename = 'index.html';       // this need to hold the relative or better the absolute *filesystem path* (not url) to the unity html file

$search = './jquery.min.js';        // this is how the jquery.min.js is embedded originally in the html file
$replace = '/unity.jquery.min.js';  // this is the absolute URL to the global js file

// a very rude method would be to just read the file content, replace the url and write it back into the file in just one line:
file_put_contents($htmlfilename, str_replace($search, $replace, file_get_contents($htmlfilename)));

.

.

But instead to do it in one line, you can use this steps one by one and optionally with validation:

    // fetch the file content
    $content = file_get_contents($htmlfilename);

    // optional validation if it has the whole content
    if (false === $content || strlen($content) != filesize($htmlfilename)) {
        // uups, we have had a read error
        ...
    }

    // replace the url of jquery.min.js
    $content2 = str_replace($search, $replace, $content);

    // optional validation if it exactly one time has changed the url in the content
    if (strlen($content2) != (strlen($content) - strlen($search) + strlen($replace))) {
        // uups, an error occured
        ...
    }

    // write it back into the html file
    $result = file_put_contents($htmlfilename, $content2, LOCK_EX);

    // optionally check if all data could be written into the file
    if (false === $result || strlen($content2) != $result) {
        // uups, we have had a write failure
        ...
    }

.

The validation for str_replace can also be done with an optional fourth parameter:

    // replace the url of jquery.min.js
    $replacementCounter = 0;
    $content = str_replace($search, $replace, $content, $replacementCounter);

    // optional validation if it exactly one time has changed the url in the content
    if (1 != $replacementCounter) {
        // uups, an error occured
        ...
    }
  • Like 4
Link to comment
Share on other sites

A module yes. But instead to hook into upload, you also can hook into page::save or page::saveReady I think. It depends upon how you have organized your site. Have those unity project pages an own template or not etc. Or do you only upload html files to those pages and not in others too.

At least it will work this or that way. :)

You will find many posts with example code here in the forums. One that hooks into upload / fileAdd is here.

page::save is here and page::saveReady here. But there are lots more :)

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...