Post

3 followers Follow
0
Avatar

Making image public

I have a calender on my website that I have my podio events app generating events for, but the images for each event are only displaying when logged into podio, is there a way to make these public?

Nick Worth Answered

Please sign in to leave a comment.

8 comments

0
Avatar

Hi Nick,

Podio doesn't offer a built-in way for making your images public. We want to keep your images secure and not accidentally exposed to the world.

With the API you can create an image proxy. Use e.g. app authentication to pull the image down, cache it locally and then make that cached file available publicly.

Does that make sense?

/Andreas

Andreas Haugstrup Pedersen 0 votes
0
Avatar

I've never done anything with a proxy but the concept you laid out makes sense... I suppose I can try to find a tutorial that follows what you're talking about here.

Nick Worth 0 votes
0
Avatar

It's pretty simple, we do it ourselves for a few images that we need publicly available.

Instead of linking directly to a static image file from your website you point the image source to a script (probably PHP script in your case?) passing an ID (probably an item id or file id from Podio).

This script will:

  1. Check if a cached version is available, serve that if it's there. Otherwise:
  2. Authenticate with Podio and download the image https://developers.podio.com/doc/files/download-file-1004147 
  3. Store a local copy of the image in the cache
  4. Serve the image from the cache

Voila :)

Andreas Haugstrup Pedersen 0 votes
0
Avatar

So I've nearly got this I think but from what I can tell that function give me all the image data, not really sure what to do with the data its self, is there a function for compiling it again? Looking up tutorials right now to better understand your approach. Makes sense so far I've just never done anything like that yet.

Nick Worth 0 votes
0
Avatar

So far I have this:

<img width="85" height="85" src="http://domain.com/wp-content/plugins/DTMP/Classes/podio/image.php?id=24622207"> 

And my Image.php file looks like this (got code from tutorial since I've never done this before): 

Podio::setup(CLIENT_ID, CLIENT_SECRET);

Podio::authenticate('app', array('app_id' => EVENT_APP_ID, 'app_token' => EVENT_APP_TOKEN));

$data = Podio::FileAttachment.find_raw( $_REQUEST['id'] );

if ($data) {

    header('Content-Type: image/jpg');

    $data;

    exit;

}

header('Location: error_image.jpg',TRUE,302);

So far no server errors from what I can tell, but also this doesn't check for cache yet, I'm just trying to get it to actually get the image like you said first and then I'll put those check downs in there. If you know of a better tutorial I could follow I'm all ears. Thanks so much for your help already as well.

Nick Worth 0 votes
0
Avatar

You will at the very least want to put a 'print' in front of that $data line. Otherwise the image data won't be sent to the browser. It also looks like you grabbed the Ruby example for the API call rather than the PHP example.

Here's a short example that stores the image locally (totally untested). Still only works for .jpg images. You'd need a second API call to PodioFile::get to fetch the image metadata (such as filesize to set a Content-length header and mimetype for the right Content-type header)

$file_id = (int)$_REQUEST['id'];

$local_file_path = 'path/to/your/file/'.$file_id.'.jpg';

$local_file_url = 'http://example.com/path/to/your/file/'.$file_id.'.jpg';

header('Content-Type: image/jpg');

// Check if the file already exists locally.

if (!file_exists($local_file_path)) {

Podio::setup(CLIENT_ID, CLIENT_SECRET);

Podio::authenticate('app', array('app_id' => EVENT_APP_ID, 'app_token' => EVENT_APP_TOKEN));

$data = PodioFile::get_raw( $file_id );

file_put_contents($local_file_path, $data);

}

// Redirect to the local image

header('Location: '.$local_image_url);

exit;

Andreas Haugstrup Pedersen 0 votes
0
Avatar

@Andreas Haugstrup Pedersen is there a reason that PodioFile::getraw() is not in the API documentation? Maybe I just can't see it (eye's aren't what they used to be :-)

Andreas Huttenrauch 0 votes
0
Avatar

Yeah, we moved things around a bit since I wrote that last reply. We want the freedom to change the URL of files so we can't have a static method for file downloading. What you should do instead is get the file object and download using the link property. It'd look like this:

$file = PodioFile::get($file_id);

// This file object now has a get_raw method:

$data = $file->get_raw(); // This is where the download happens

file_put_contents($local_file_path, $data);

So it's not in the API reference as it's not an API operation. It's just a convenience method I added to the PHP library (and the Ruby library)

Andreas Haugstrup Pedersen 0 votes