I'm working on a plugin that creates auto crops of a selected image, but I am unsure of the proper steps to create and upload an image programatically. I am hoping that Local asset sources and Remote asset sources can be treated the same way. If not, then I would like to create that abstraction.

My understanding of this at the moment is that I need to both insert the file and save an asset file record. So I am first getting a local copy of the original asset that I will create crops from via <assetSource>->getLocalCopy(), and using that local path with craft()->assets->insertFileByLocalPath().

This works well if I use a Local asset source to store my cropped images. However, when storing my crops on Rackspace, I run into an issue where a naming conflict is not resolved.

I am calling insertFileByLocalPath() with AssetConflictResolution::Replace. I've noticed that fileId is not set in BaseAssetSourceType->insertFileByPath() until the response is successful, which won't happen while there is a naming conflict. Instead the response returns back to craft()->assets->insertFileByLocalPath(), which then tries to access fileId via $response->getDataItem('fileId') which has not been set. I then get an error stating:

Fatal error: Call to a member function getFolder() on null in AssetsService on line 1334.

The failure occurs in AssetService _mergeUploadedFiles() after trying to getFileById() where the id is null, and then calling getFolder() on null.

I am not sure if this is the best place to put this, if this a bug within Craft, or if I am going about creating and uploading Assets in the wrong way.

Are my steps to upload images correct thus far? Can all asset sources be treated in the same way? Any guidance or critique would be greatly appreciated.

  • 1
    I'm experiencing the same issue. Did anything ever come of this?
    – keito
    Nov 23, 2015 at 14:55
  • I've also had this issue myself, I'm not sure it was ever fixed.
    – Jay
    Mar 17, 2016 at 14:15
  • To update: I ended up handling the conflict and renaming myself which is annoying but works. I'm going to test in 2.5 soon and see if they have fixed this, and I'll report back with any findings.
    – kcolls
    Mar 18, 2016 at 16:15


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.