Skip to content

Commit

Permalink
Merge pull request #5117 from queoGmbH/bugfix/prevent-multiple-import…
Browse files Browse the repository at this point in the history
…s-of-same-asset

BUGFIX: Prevent multiple imports of the same remote asset in the frontend
  • Loading branch information
ahaeslich authored Jun 5, 2024
2 parents 0eb3c69 + f78e0d4 commit ad66c9e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Neos.Media.Browser/Resources/Private/Layouts/Default.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
<div class="neos-media-content{f:if(condition: '{tags -> f:count()} > 25', then: ' neos-media-aside-condensed')}">
<div class="neos-media-assets">
<div class="neos-notification-container">
<div id="neos-notification-container" class="neos-notification-container">
<f:render partial="FlashMessages"/>
</div>
<f:render section="Content"/>
Expand Down
6 changes: 6 additions & 0 deletions Neos.Media.Browser/Resources/Private/Translations/en/Main.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@
<trans-unit id="createMissingVariants" xml:space="preserve">
<source>Create missing variants</source>
</trans-unit>
<trans-unit id="assetImport.importInfo" xml:space="preserve">
<source>Asset is being imported. Please wait.</source>
</trans-unit>
<trans-unit id="assetImport.importInProcess" xml:space="preserve">
<source>Import still in process. Please wait.</source>
</trans-unit>
</body>
</file>
</xliff>
114 changes: 83 additions & 31 deletions Neos.Media.Browser/Resources/Public/JavaScript/select.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,84 @@
window.addEventListener('DOMContentLoaded', (event) => {
$(function() {
if (window.parent !== window && window.parent.NeosMediaBrowserCallbacks) {
// we are inside iframe
$('.asset-list').on('click', '[data-asset-identifier]', function(e) {
if (
$(e.target).closest('a, button').not('[data-asset-identifier]').length === 0 &&
window.parent.NeosMediaBrowserCallbacks &&
typeof window.parent.NeosMediaBrowserCallbacks.assetChosen === 'function'
) {
let localAssetIdentifier = $(this).attr('data-local-asset-identifier');
if (localAssetIdentifier !== '') {
window.parent.NeosMediaBrowserCallbacks.assetChosen(localAssetIdentifier);
} else {
$.post(
$('link[rel="neos-media-browser-service-assetproxies-import"]').attr('href'),
{
assetSourceIdentifier: $(this).attr('data-asset-source-identifier'),
assetIdentifier: $(this).attr('data-asset-identifier'),
__csrfToken: $('body').attr('data-csrf-token')
},
function(data) {
window.parent.NeosMediaBrowserCallbacks.assetChosen(data.localAssetIdentifier);
}
);
}
e.preventDefault();
}
});
}
});
window.addEventListener('DOMContentLoaded', () => {
(function () {
const NeosMediaBrowserCallbacks = window.parent.NeosMediaBrowserCallbacks;
const NeosCMS = window.NeosCMS;

if (window.parent === window || !NeosCMS || !NeosMediaBrowserCallbacks || typeof NeosMediaBrowserCallbacks.assetChosen !== 'function') {
return;
}

function importAsset(asset) {
const params = new URLSearchParams();
params.append('assetSourceIdentifier', asset.dataset.assetSourceIdentifier);
params.append('assetIdentifier', asset.dataset.assetIdentifier);
params.append('__csrfToken', document.querySelector('body').dataset.csrfToken);

fetch(
document
.querySelector('link[rel="neos-media-browser-service-assetproxies-import"]')
.getAttribute('href'),
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
method: 'POST',
credentials: 'include',
body: params.toString(),
}
)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then((data) => {
NeosMediaBrowserCallbacks.assetChosen(data.localAssetIdentifier);
asset.removeAttribute('data-import-in-process');
})
.catch((error) => {
NeosCMS.Notification.error(NeosCMS.I18n.translate(
'assetImport.importError',
'Asset could not be imported. Please try again.',
'Neos.Media.Browser'
), error);
console.error('Error:', error);
})
}

const assets = document.querySelectorAll('[data-asset-identifier]');
assets.forEach((asset) => {
asset.addEventListener('click', (e) => {
const assetLink = e.target.closest('a[data-asset-identifier], button[data-asset-identifier]');
if (!assetLink) {
return;
}

const localAssetIdentifier = asset.dataset.localAssetIdentifier;
if (localAssetIdentifier !== '' && !NeosCMS.isNil(localAssetIdentifier)) {
NeosMediaBrowserCallbacks.assetChosen(localAssetIdentifier);
} else {
if (asset.dataset.importInProcess !== 'true') {
asset.dataset.importInProcess = 'true';
const message = NeosCMS.I18n.translate(
'assetImport.importInfo',
'Asset is being imported. Please wait.',
'Neos.Media.Browser'
);
NeosCMS.Notification.ok(message);

importAsset(asset);
} else {
const message = NeosCMS.I18n.translate(
'assetImport.importInProcess',
'Import still in process. Please wait.',
'Neos.Media.Browser'
);
NeosCMS.Notification.warning(message);
}
}
e.preventDefault();
});
});
})();
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getCollectionValueByPath, isNil } from "../Helper";

const hasConfiguration = !isNil(window.NeosCMS?.Configuration);
let hasConfiguration = !isNil(window.NeosCMS?.Configuration);

const init = () => {
if (isNil(window.NeosCMS)) {
Expand All @@ -9,6 +9,7 @@ const init = () => {

if (isNil(window.NeosCMS.Configuration)) {
window.NeosCMS.Configuration = {};
hasConfiguration = true;
}

// append xliff uri
Expand Down

0 comments on commit ad66c9e

Please sign in to comment.