Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMPROVEMENT] Assets:upload zip archive-option to overwrite files #420

144 changes: 86 additions & 58 deletions public/js/pimcore/asset/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -1070,66 +1070,94 @@
},

uploadZip: function (tree, record) {

pimcore.helpers.uploadDialog(Routing.generate('pimcore_admin_asset_importzip', {parentId: record.id}), "Filedata", function (response) {
// this.attributes.reference
var res = Ext.decode(response.response.responseText);
pimcore.helpers.addTreeNodeLoadingIndicator("asset", record.get("id"));

this.downloadProgressBar = new Ext.ProgressBar({
text: t('initializing')
});

this.downloadProgressWin = new Ext.Window({
title: t("upload_zip"),
layout:'fit',
width:200,
bodyStyle: "padding: 10px;",
closable:false,
plain: true,
items: [this.downloadProgressBar],
listeners: pimcore.helpers.getProgressWindowListeners()
});

this.downloadProgressWin.show();

var pj = new pimcore.tool.paralleljobs({
success: function (jobId) {
if(this.downloadProgressWin) {

const uploadFunction = function(allowOverwrite) {
pimcore.helpers.uploadDialog(Routing.generate('pimcore_admin_asset_importzip', {parentId: record.id, allowOverwrite: allowOverwrite ? 'true' : 'false' }), "Filedata", function (response) {
// this.attributes.reference
var res = Ext.decode(response.response.responseText);
pimcore.helpers.addTreeNodeLoadingIndicator("asset", record.get("id"));

this.downloadProgressBar = new Ext.ProgressBar({
text: t('initializing')
});

this.downloadProgressWin = new Ext.Window({
title: t("upload_zip"),
layout: 'fit',
width: 200,
bodyStyle: "padding: 10px;",
closable: false,
plain: true,
items: [this.downloadProgressBar],
listeners: pimcore.helpers.getProgressWindowListeners()
});

this.downloadProgressWin.show();

var pj = new pimcore.tool.paralleljobs({
success: function (jobId) {
if (this.downloadProgressWin) {
this.downloadProgressWin.close();
}

this.downloadProgressBar = null;
this.downloadProgressWin = null;

pimcore.elementservice.refreshNodeAllTrees("asset", record.get("id"));
}.bind(this, res.jobId),
update: function (currentStep, steps, percent) {
if (this.downloadProgressBar) {
var status = currentStep / steps;
this.downloadProgressBar.updateProgress(status, percent + "%");
}
}.bind(this),
failure: function (message) {
this.downloadProgressWin.close();
}

this.downloadProgressBar = null;
this.downloadProgressWin = null;

pimcore.elementservice.refreshNodeAllTrees("asset", record.get("id"));
}.bind(this, res.jobId),
update: function (currentStep, steps, percent) {
if(this.downloadProgressBar) {
var status = currentStep / steps;
this.downloadProgressBar.updateProgress(status, percent + "%");
}
}.bind(this),
failure: function (message) {
this.downloadProgressWin.close();
pimcore.elementservice.refreshNodeAllTrees("asset", record.get("id"));
pimcore.helpers.showNotification(t("error"), t("error"),
"error", t(message));
}.bind(this),
jobs: res.jobs
});
}.bind(this), function (res) {
var response = Ext.decode(res.response.responseText);
if (response && response.success === false) {
pimcore.helpers.showNotification(t("error"), response.message, "error",
res.response.responseText);
} else {
pimcore.helpers.showNotification(t("error"), res, "error",
res.response.responseText);
pimcore.elementservice.refreshNodeAllTrees("asset", record.get("id"));
pimcore.helpers.showNotification(t("error"), t("error"),
"error", t(message));
}.bind(this),
jobs: res.jobs
});
}.bind(this), function (res) {
var response = Ext.decode(res.response.responseText);
if (response && response.success === false) {
pimcore.helpers.showNotification(t("error"), response.message, "error",
res.response.responseText);
} else {
pimcore.helpers.showNotification(t("error"), res, "error",
res.response.responseText);
}

pimcore.elementservice.refreshNodeAllTrees("asset", record.parentNode.get("id"));
}.bind(this));
}

const messageBox = new Ext.window.MessageBox({
layout: {
type: 'vbox',
align: 'center'
}

pimcore.elementservice.refreshNodeAllTrees("asset", record.parentNode.get("id"));
}.bind(this));
});

messageBox.show({
title: t('overwrite_zip_files'),
msg: t('zip_upload_want_to_overwrite'),
buttons: Ext.Msg.OK & Ext.Msg.NO,
buttonText: {
yes: t('zip_upload_want_to_overwrite_yes_option'),
no: t('zip_upload_want_to_overwrite_no_option')
},
prompt: false,
icon: Ext.MessageBox.QUESTION,
fn: function (action) {
if (action === 'yes') {
uploadFunction(action === 'yes'); // currently visible message box if not visible anymore after clicking a button -> action for current message box gets executed here instead of in above loop
} else {
uploadFunction()
}
}
});
},

enableHtml5Upload: function (node, rowIdx, out) {
Expand Down
25 changes: 18 additions & 7 deletions src/Controller/Admin/Asset/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2091,6 +2091,7 @@ public function importZipAction(Request $request, TranslatorInterface $translato
'limit' => $filesPerJob,
'jobId' => $jobId,
'last' => (($i + 1) >= $jobAmount) ? 'true' : '',
'allowOverwrite' => $request->get('allowOverwrite') ? $request->get('allowOverwrite') : 'false',
],
]];
}
Expand Down Expand Up @@ -2154,15 +2155,25 @@ public function importZipFilesAction(Request $request, Filesystem $filesystem):
$parent = Asset\Service::createFolderByPath($parentPath);

// check for duplicate filename
$filename = $this->getSafeFilename($parent->getRealFullPath(), $filename);
if ($request->get('allowOverwrite') && $request->get('allowOverwrite') !== 'true') {
$filename = $this->getSafeFilename($parent->getRealFullPath(), $filename);
}

if ($parent->isAllowed('create')) {
$asset = Asset::create($parent->getId(), [
'filename' => $filename,
'sourcePath' => $tmpFile,
'userOwner' => $this->getAdminUser()->getId(),
'userModification' => $this->getAdminUser()->getId(),
]);
if ($request->get('allowOverwrite') && $request->get('allowOverwrite') === 'true'
&& Asset\Service::pathExists($parent->getRealFullPath().'/'.$filename))
{
$asset = Asset::getByPath($parent->getRealFullPath().'/'.$filename);
$asset->setStream(fopen($tmpFile, 'rb', false, File::getContext()));
$asset->save();
} else {
Asset::create($parent->getId(), [
'filename' => $filename,
'sourcePath' => $tmpFile,
'userOwner' => $this->getAdminUser()->getId(),
'userModification' => $this->getAdminUser()->getId(),
]);
}

@unlink($tmpFile);
} else {
Expand Down
6 changes: 5 additions & 1 deletion translations/admin.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1020,5 +1020,9 @@ male: Male
female: Female
pdf_js_unsafe: This PDF file contains JavaScript. If you want to view it, please download and open it in your local PDF viewer.
pdf_scan_in_progress: 'Preview not available: PDF is being scanned. This may take a while.'
overwrite_zip_files: 'Overwrite with zip files'
zip_upload_want_to_overwrite: 'Do you want to overwrite the files?'
zip_upload_want_to_overwrite_yes_option: 'Yes'
zip_upload_want_to_overwrite_no_option: 'No'
invalid_option: 'Invalid Option field [ {field} ]: Please choose a valid option for select / multiselect field [ {field} ]. Current value: "{option}"'
respect_timezone: 'Respect timezone'
respect_timezone: 'Respect timezone'
Loading