Hi! this is an awesome module, thanks!

I had a need to use relative paths to the public file paths, for example:
http://cdn.example.com/pictures/avatar.jpg

Because my CDN is an Amazon S3 bucket mounted in the files directory of Drupal.

So I made some adjust to make that happen:
$path = str_replace(variable_get('file_public_path', conf_path() . '/files') . '/', '', $path);

But! thats not all!!, if some of those where an image generated by the image style, wouldn't work, because it needs to pass through the original path(http://cdn.example.com/sites/default/files/pictures/avatar.jpg) to generate the new image for that specifically style.
This can be solve when the path is being build for the first and unique time, using the image_style_create_derivative() function from the image styles core module.

// Generate the image for the given style the first time.
$file_public_path_styles_reg = "/^(.*)\:\/\/styles\/(.[^\/]*)\/.[^\/]*\/(.*)/";
if (variable_get(CDN_DRUPAL_PUBLIC_FILE_PATH_REMOVE, FALSE) == TRUE && preg_match($file_public_path_styles_reg, $original_uri, $matches)) {

  $image_style = $matches[2];
  $schema = $matches[1];
  $file_source_uri = $schema . '://' . $matches[3];
  $file_source_path = drupal_realpath($file_source_uri);
  
   if (!file_exists($file_source_path)) {
    image_style_create_derivative(image_style_load($image_style), $file_source_uri, $original_uri);
  }
}

This is just an idea for this kind of CDN, to me it works just fine!
Remember this will be an option now in the details configuration tab after applying the patch.

Cheers! and let me know how this approach can be better :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sebas5384’s picture

Status: Active » Needs review
FileSize
3.57 KB
sebas5384’s picture

I made an adjust and rerolled the patch to a new one.

Wim Leers’s picture

Version: 7.x-2.5 » 7.x-2.x-dev
Status: Needs review » Needs work

Thanks for the patch! :)

1. For any new feature to be considered, it should be covered by tests if feasible. In this case, that is definitely feasible.
2. I very much dislike the explicit calling of image.module functions. In fact, this is unacceptable. Other modules that may generate files on the fly would also need to have code there. That's going to become an unmaintainable mess. Instead, you should be using _cdn_basic_farfuture_generate_file().
3. Why not use Amazon CloudFront?
4. How are you using S3? If you were using a custom stream wrapper for that, it'd be a S3 URL automatically. Related: http://drupal.org/project/storage_api. There already is an issue for integration with that too: #875632: Storage API + CDN.

Wim Leers’s picture

Status: Needs work » Closed (works as designed)

I'm afraid this is not really an acceptable feature/patch. It solves the problem in an incorrect way, as already alluded to in #3.

bgilhome’s picture

Status: Closed (works as designed) » Needs review
FileSize
992 bytes

Sorry to re-open this issue - just wondering what your thoughts are on the way I've integrated a workaround for this. I have a fuse-style mount at my public files directory location to my cloud storage - hence asset URLs to my CDN need to be in the form http://example.com/relative/path/from/files/directory

I've patched cdn.basic.inc to parse query-style options appended to CDN URLs in the CDN mapping textarea, options which can alter the resultant mapped URL. eg., for my use case, I've included an option 'relative' that strips the public files directory from the served CDN URL. So, if I specify my CDN mapping as "http://example.com?relative | .jpeg", any jpeg images in the public files directory will map to http://example.com/relative/path/from/files/directory

As for the imagecache issue, I've used ImageInfo Cache to generate styled images on upload so they're present in the CDN.

panche’s picture

Sorry. Wrong click

Wim Leers’s picture

Status: Needs review » Closed (works as designed)

Sorry, no, this should not require hacks in the CDN module. You should use the stream wrapper API that PHP/Drupal provides. Then it'd be supported automatically, see http://cgit.drupalcode.org/cdn/tree/cdn.module?id=c9b98bf5c8254b0c6d721f...