Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Added option to skip existing images
Browse files Browse the repository at this point in the history
Added import timer
Refactored the setting of data to prevent complete rewrites
  • Loading branch information
jezzdk committed Sep 15, 2021
1 parent 9bfcaa8 commit 7a4b3d8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 13 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ return [
*/
'download_images' => true,
/*
* Whether to skip download of an image if it already exist. The default is 'false'.
*/
'skip_existing_images' => false,
/*
* Enable image overwriting. When set to false, a new image are created with a timestamp suffix, if the image already exists. The default is 'false'.
*/
Expand Down
5 changes: 5 additions & 0 deletions config/statamic-wp-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
*/
'download_images' => true,

/*
* Whether to skip download of an image if it already exist. The default is 'false'.
*/
'skip_existing_images' => false,

/*
* Enable image overwriting. When set to false, a new image are created with a timestamp suffix, if the image already exists. The default is 'false'.
*/
Expand Down
2 changes: 1 addition & 1 deletion dist/js/addon.js

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions resources/js/components/Importer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default {
showAllPages: false,
showCollections: [],
showTaxonomies: [],
counter: null,
hours: 0,
minutes: 0,
seconds: 0
}
},
Expand Down Expand Up @@ -55,6 +59,8 @@ export default {
this.$progress.start('wp-import')
this.startTimer()
fetch(cp_url('wp-import/import'), {
method: 'POST',
headers: {
Expand All @@ -78,6 +84,8 @@ export default {
this.$progress.complete('wp-import')
this.stopTimer()
return response.json();
}).then((data) => {
console.log(data)
Expand Down Expand Up @@ -190,6 +198,26 @@ export default {
}
return totalEntries
},
startTimer () {
this.minutes = this.checkSingleDigit(0)
this.seconds = this.checkSingleDigit(0)
this.counter = setInterval(() => {
const date = new Date(0, 0, 0, 0, 0, parseInt(this.seconds) + 1)
this.hours = date.getHours()
this.minutes = this.checkSingleDigit(date.getMinutes())
this.seconds = this.checkSingleDigit(date.getSeconds())
}, 1000)
},
stopTimer () {
clearInterval(this.counter)
},
checkSingleDigit (digit) {
return ('0' + digit).slice(-2)
}
}
};
Expand Down
35 changes: 31 additions & 4 deletions resources/views/summary.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,17 @@
<div>
<div class="card mb-3">
<h2 class="mb-2">Importing</h2>
<div class="loading loading-basic">
<span class="icon icon-circular-graph animation-spin"></span> Please wait
<div class="w-full flex justify-between items-center">
<div class="loading loading-basic">
<span class="icon icon-circular-graph animation-spin"></span> Please wait
</div>
<div class="flex text-grey-50 text-sm">
<div v-show="hours > 0">@{{ hours }}</div>
<div v-show="hours > 0">:</div>
<div>@{{ minutes }}</div>
<div>:</div>
<div>@{{ seconds }}</div>
</div>
</div>
</div>
</div>
Expand All @@ -151,7 +160,16 @@
<div>
<div class="card mb-3">
<h2 class="mb-2">Import complete</h2>
<p>Import has completed</p>
<div class="w-full flex justify-between items-center">
<p>Import has completed</p>
<div class="flex text-grey-50 text-sm">
<div v-show="hours > 0">@{{ hours }}</div>
<div v-show="hours > 0">:</div>
<div>@{{ minutes }}</div>
<div>:</div>
<div>@{{ seconds }}</div>
</div>
</div>
</div>
</div>
</template>
Expand All @@ -160,7 +178,16 @@
<div>
<div class="card">
<h2 class="mb-2">Import failed</h2>
<p>@{{ importError }}</p>
<div class="w-full flex justify-between items-center">
<p>@{{ importError }}</p>
<div class="flex text-grey-50 text-sm">
<div v-show="hours > 0">@{{ hours }}</div>
<div v-show="hours > 0">:</div>
<div>@{{ minutes }}</div>
<div>:</div>
<div>@{{ seconds }}</div>
</div>
</div>
</div>
</div>
</template>
Expand Down
31 changes: 23 additions & 8 deletions src/Helpers/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ private function createTaxonomies()
$taxonomy = Taxonomy::make($taxonomy_slug);
}

$taxonomy->data($taxonomy_data);
foreach ($taxonomy_data as $key => $value) {
$taxonomy->set($key, $value);
}

$taxonomy->save();
}
Expand All @@ -129,7 +131,11 @@ private function createTaxonomyTerms()
$term = Term::make($term_slug)->taxonomy($taxonomy_slug);
}

$term->data($term_data)->save();
foreach ($term_data as $key => $value) {
$term->set($key, $value);
}

$term->save();
}
}
}
Expand Down Expand Up @@ -177,9 +183,12 @@ private function createEntries()
}

$entry->date($meta['order']);
$entry->data(array_merge($meta['data'], [
'slug' => $slug
]));

array_set($meta, 'data.slug', $slug);

foreach ($meta['data'] as $key => $value) {
$entry->set($key, $value);
}

if (config('statamic-wp-import.download_images')) {
$asset = $this->downloadAsset($meta['data']['featured_image_url'] ?? '', $collection, $slug);
Expand Down Expand Up @@ -216,9 +225,11 @@ private function createPages()
$page = Entry::make()->collection('pages')->slug($slug);
}

$page->data(array_merge($meta['data'], [
'slug' => $slug
]));
array_set($meta, 'data.slug', $slug);

foreach ($meta['data'] as $key => $value) {
$page->set($key, $value);
}

if (config('statamic-wp-import.download_images')) {
$asset = $this->downloadAsset($meta['data']['featured_image_url'] ?? '', 'pages', $slug);
Expand Down Expand Up @@ -255,6 +266,10 @@ private function downloadAsset(string $url = null, string $collection, string $s

$asset = $assetContainer->makeAsset("{$collection}/{$slug}/{$originalImageName}");

if ($asset->exists() && config('statamic-wp-import.skip_existing_images')) {
return $asset;
}

if ($asset->exists() && config('statamic-wp-import.overwrite_images')) {
$asset->delete();
}
Expand Down

0 comments on commit 7a4b3d8

Please sign in to comment.