<p><strong>Context</strong>: For a redesign and port to CraftCMS, I need to import the content from a blog hosted on another platform with a lot of content (over 500 posts and over 3000 images).</p>

<p><strong>Strategy</strong>: To import the images from external URLS, I have written a simple "dedicated" plugin which uses the <a href="https://craftcms.stackexchange.com/questions/10678/how-to-upload-assets-without-user-interaction">technique described here</a> and relies on <a href="https://craftcms.com/classreference/services/AssetsService#insertFileByLocalPath-detail" rel="nofollow">craft()->assets->insertFileByLocalPath()</a>.</p>

<p><strong>Issue</strong>: To make sure each import uploads the latest version of the images, I pass <code>AssetConflictResolution::Replace</code> as fourth argument to the method. This works perfectly the first time I run the import, but dies when files were uploaded before and there is a filename conflict:</p>

<pre><code>Trying to get property of non-object
[...] $source = craft()-&gt;assetSources-&gt;getSourceTypeById($theNewFile-&gt;sourceId);
</code></pre>

<p>I am not sure why this error occurs, but I suspect it is because newly imported files are not uploaded yet at the time Craft tries to resolve the conflict.</p>

<p><strong>Possible solutions</strong>: I tried to handle conflicts manually, by first deleting the existing file with <code>craft()-&gt;assets-&gt;deleteFiles($response-&gt;getDataItem('fileId'), true)</code>, and then re-uploading the new file with <code>insertFileByLocalPath</code> but no success...</p>

<p>Since the target assets source is reserved for the imported images, I also thought about dumping all files in the target assets source before the import starts. But that would cause problems if I want to import only a subset of all the post images (very likely since the import needs time to execute).</p>

<p><strong>Question</strong>: How can I work around this error?</p>

<p>The solution should make sure:</p>

<ol>
<li>newly uploaded files always replaces existing files with the same filename.</li>
<li>partial imports don't remove unrelated files that were uploaded before.</li>
</ol>

CC BY-SA 3.0
2016-10-31T06:01:05.923Z
Have you observed what the value of "$theNewFile" is when uploading for a second time? The line before that is: $theNewFile = $this->getFileById($theNewFileId);  so perhaps the issue is actually with the getFileById method.
Jarrett Barnett
CC BY-SA 3.0
2016-10-31T07:20:23.850Z
Without seeing the actual plugin code (at least the method) it's hard to tell what's going on. Can you post some code on github and link to it for reference or, better yet, just post it here?
Andris Sevcenko
CC BY-SA 4.0
2019-02-15T07:41:09.907Z
Did you find a solution? Experiencing something alike and are curious of what you did to fix it
Erik Weber
<plugin-development><assets><upload><import>
Upload assets manually: replace existing files
# Upload assets manually: replace existing files

(Asked by: username_0 on 2016-10-30)

Context: For a redesign and port to CraftCMS, I need to import the content from a blog hosted on another platform with a lot of content (over 500 posts and over 3000 images).
Strategy: To import the images from external URLS, I have written a simple "dedicated" plugin which uses the technique described here and relies on craft()->assets->insertFileByLocalPath().
Issue: To make sure each import uploads the latest version of the images, I pass AssetConflictResolution::Replace as fourth argument to the method. This works perfectly the first time I run the import, but dies when files were uploaded before and there is a filename conflict:
Trying to get property of non-object
[...] $source = craft()->assetSources->getSourceTypeById($theNewFile->sourceId);

I am not sure why this error occurs, but I suspect it is because newly imported files are not uploaded yet at the time Craft tries to resolve the conflict.
Possible solutions: I tried to handle conflicts manually, by first deleting the existing file with craft()->assets->deleteFiles($response->getDataItem('fileId'), true), and then re-uploading the new file with insertFileByLocalPath but no success...
Since the target assets source is reserved for the imported images, I also thought about dumping all files in the target assets source before the import starts. But that would cause problems if I want to import only a subset of all the post images (very likely since the import needs time to execute).
Question: How can I work around this error?
The solution should make sure:

newly uploaded files always replaces existing files with the same filename.
partial imports don't remove unrelated files that were uploaded before.

**Tags:** plugin-development, assets, upload, import

> **Comments:**
> **username_1**: Have you observed what the value of "$theNewFile" is when uploading for a second time? The line before that is: $theNewFile = $this->getFileById($theNewFileId);  so perhaps the issue is actually with the getFileById method.  
> **username_2**: Without seeing the actual plugin code (at least the method) it's hard to tell what's going on. Can you post some code on github and link to it for reference or, better yet, just post it here?  
> **username_3**: Did you find a solution? Experiencing something alike and are curious of what you did to fix it  

---

## 0 answers

