Jump to content

Make assets from different pages accessible from a single URL


charger
 Share

Recommended Posts

I need assets (PDF files) from multiple pages to be accessible from a single URL. That means, instead of

http://www.domain.com/assets/files/1011/example1.pdf
http://www.domain.com/assets/files/1012/example2.pdf
...
it should be e.g.

so without the page ID in the URL.

The reason I need it this way, is because:

  1. I can't use a single page to save all files (PDFs) in it, as there are additional fields to every PDF. That means I need PageTable or Repeater, and both create new pages for every PDF
  2. There's a 3rd party service that links from external pages to the PDFs. That service however doesn't know about the page ID in the URLs, it only knows the filename of the PDFs.

I've found the following topics in the forum that somewhat touch my subject, but I couldn't make them work for me:

https://processwire.com/talk/topic/8312-creating-my-own-folders-for-image-assets/

https://processwire.com/talk/topic/4127-can-assets-folder-be-moved-to-another-domainsubdomain/

Any idea on how I could solve this?

Link to comment
Share on other sites

As long as you don’t need to physically store the files in one central directory, I’d create a page that accepts the file names as urlSegments, and serve them from there. The file names would have to be unique anyway for your proposal to work, so you should have no problem fetching them. The only problem would be that this solution does not guarantee unique upload file names, because the files would still be in separate directories. You could ignore this or hook into the file upload somewhere to make sure.

E. g. you may do this:

$filename = $sanitizer->selectorValue($input->urlSegment1);
$p = $pages->get('images=' . $filename);
if ($p->id > 0) 
    header('Location: ' . $p->images->get($filename)->url);
else
    throw new Wire404Exception();
die();

Bit crude but you get the idea.

  • Like 2
Link to comment
Share on other sites

  • 3 weeks later...

Thanks for your answers, I've used the following (slightly modified code) in my template to get it work:

if(!$input->urlSegment1) throw new WireException(); // check for url segment
$filename = $sanitizer->pageName($input->urlSegment1); // sanitize url segment (filename)
$p = $pages->get("files=$filename"); // search all pages for $filename in field files
if(!$p->id) throw new Wire404Exception(); // check if page exists
$session->redirect($pages->get($p->id)->job_pdf->url); // redirect to file

For some reason, the additional viewable() check for $p always resulted in a 404 error. So I removed it.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...