Download & Extend

Programmatically attaching files which don't (yet) exist

Project:FileField
Version:6.x-3.2
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

I'm working on a module which imports listings from a remote database into local nodes. The listings may include an arbitrary number of images, which are only available by separate remote calls. As the module may import several thousand listings at once, and each listing may have from zero to a couple dozen images, and it's not guaranteed that any particular node's images are going to ever be viewed even once before the node is purged anyway, I've added a menu handler to my module which fires if an image is requested which isn't stored locally yet, and only then tries to fetch it from the server and store it locally for later use.

The trick is that I'd like these images to be attached to the node through an ImageField/FileField so that they can easily be used with ImageCache, Thickbox, etc, and so far I haven't been successful in programmatically attaching these images to nodes - I'm not sure yet, but I suspect it's because the images don't exist. Is there a practical way to go about this? I'm still poking around and trying to find a way myself, but I suspect any method I find is going to be rather hackish and maybe not the best way to solve this problem, so I'd appreciate any insight others may have. Thanks.

Comments

#1

I have the same kind of request. Did you find an answer?

#2

Heh, yeah, I did find a solution, though it's a hack. I think it's one of the most epic hacks of my life so far, though.

In a nutshell, I created a 1x1-pixel white JPEG image to use as a placeholder. When I download a listing from the remote database and it says it has X pictures attached to it, I attach X copies of the placeholder image, but each placeholder image has the same filename as what the real image would have.

I then created an ImageCache action which, when triggered, checks the file size of the image it's being asked to operate upon. If it is not the same size as the placeholder image's size, it does nothing; otherwise, it connects to the server, downloads the real image, and replaces that copy of the placeholder image with the real image (which is pretty much assured to have a larger filesize than the placeholder). It then passes on the image data to ImageCache for the next ImageCache action, if any, to act upon.

It's hacky, but this way, the image is never downloaded until just before it is about to be viewed for the first time. It's a bit of extra work to get configured for our users, but not too much since odds are that they're going to be using ImageCache to scale down the images anyway.

#3

Status:active» closed (fixed)

<?php $open_issues--; ?>

nobody click here