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.