Skip to content

Commit

Permalink
Merge commit '22c7bbf' into 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Minnee committed May 25, 2013
2 parents 00e09f1 + 22c7bbf commit 34bfc86
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 28 deletions.
3 changes: 2 additions & 1 deletion _config/uploadfield.yml
Expand Up @@ -13,4 +13,5 @@ UploadField:
downloadTemplateName: 'ss-uploadfield-downloadtemplate'
fileEditFields:
fileEditActions:
fileEditValidator:
fileEditValidator:
overwriteWarning: true # Warning before overwriting existing file (only relevant when Upload: replaceFile is true)
6 changes: 3 additions & 3 deletions css/AssetUploadField.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/en/reference/uploadfield.md
Expand Up @@ -225,9 +225,9 @@ You can also configure the underlying `[api:Upload]` class, by using the YAML co

:::yaml
Upload:
# Globally disables automatic renaming of files
# Globally disables automatic renaming of files and displays a warning before overwriting an existing file
replaceFile: true

## TODO: Using the UploadField in a frontend form

*At this moment the UploadField not yet fully supports being used on a frontend
Expand Down
40 changes: 22 additions & 18 deletions filesystem/Upload.php
Expand Up @@ -153,31 +153,35 @@ public function load($tmpFile, $folderPath = false) {

// if filename already exists, version the filename (e.g. test.gif to test1.gif)
if(!$this->replaceFile) {
while(file_exists("$base/$relativeFilePath")) {
$i = isset($i) ? ($i+1) : 2;
$oldFilePath = $relativeFilePath;
// make sure archives retain valid extensions
if(substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.gz')) == '.tar.gz' ||
substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.bz2')) == '.tar.bz2') {
$relativeFilePath = preg_replace('/[0-9]*(\.tar\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '.') !== false) {
$relativeFilePath = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '_') !== false) {
$relativeFilePath = preg_replace('/_([^_]+$)/', '_'.$i, $relativeFilePath);
} else {
$relativeFilePath .= '_'.$i;
}
if($oldFilePath == $relativeFilePath && $i > 2) {
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
}
}
while(file_exists("$base/$relativeFilePath")) {
$i = isset($i) ? ($i+1) : 2;
$oldFilePath = $relativeFilePath;
// make sure archives retain valid extensions
if(substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.gz')) == '.tar.gz' ||
substr($relativeFilePath, strlen($relativeFilePath) - strlen('.tar.bz2')) == '.tar.bz2') {
$relativeFilePath = preg_replace('/[0-9]*(\.tar\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '.') !== false) {
$relativeFilePath = preg_replace('/[0-9]*(\.[^.]+$)/', $i . '\\1', $relativeFilePath);
} else if (strpos($relativeFilePath, '_') !== false) {
$relativeFilePath = preg_replace('/_([^_]+$)/', '_'.$i, $relativeFilePath);
} else {
$relativeFilePath .= '_'.$i;
}
if($oldFilePath == $relativeFilePath && $i > 2) {
user_error("Couldn't fix $relativeFilePath with $i tries", E_USER_ERROR);
}
}
} else {
//reset the ownerID to the current member when replacing files
$this->file->OwnerID = (Member::currentUser() ? Member::currentUser()->ID : 0);
}

if(file_exists($tmpFile['tmp_name']) && copy($tmpFile['tmp_name'], "$base/$relativeFilePath")) {
$this->file->ParentID = $parentFolder ? $parentFolder->ID : 0;
// This is to prevent it from trying to rename the file
$this->file->Name = basename($relativeFilePath);
$this->file->write();
$this->extend('onAfterLoad', $this->file); //to allow extensions to e.g. create a version after an upload
return true;
} else {
$this->errors[] = _t('File.NOFILESIZE', 'Filesize is zero bytes.');
Expand Down
17 changes: 16 additions & 1 deletion forms/UploadField.php
Expand Up @@ -141,7 +141,11 @@ class UploadField extends FileField {
* @example 'getCMSValidator'
* @var string
*/
'fileEditValidator' => null
'fileEditValidator' => null,
/**
* Show a warning when overwriting a file
*/
'overwriteWarning' => true,
);

/**
Expand Down Expand Up @@ -416,6 +420,17 @@ public function Field($properties = array()) {
if (is_numeric($config['maxNumberOfFiles']) && $this->getItems()->count()) {
$configOverwrite['maxNumberOfFiles'] = $config['maxNumberOfFiles'] - $this->getItems()->count();
}

//get all the existing files in the current folder
if ($this->getConfig('overwriteWarning')) {
$folder = Folder::find_or_make($this->getFolderName());
$files = glob( $folder->getFullPath() . '/*' );
$config['existingFiles'] = array_map("basename", $files);;

//add overwrite warning error message to the config object sent to Javascript
$config['errorMessages']['overwriteWarning'] =
_t('UploadField.OVERWRITEWARNING','File with the same name already exists');
}

$config = array_merge($config, $this->ufConfig, $configOverwrite);

Expand Down
33 changes: 33 additions & 0 deletions javascript/UploadField.js
Expand Up @@ -48,6 +48,39 @@

return result;
},
_onSend: function (e, data) {
//check the array of existing files to see if we are trying to upload a file that already exists
var that = this;
var config = $('div.ss-upload').entwine('ss').getConfig();
var existingFiles = [];
if (typeof (config.existingFiles) !== "undefined") {
existingFiles = config.existingFiles;
}

var fileExists = false;
jQuery.each(existingFiles,function(){
if ($(this)[0].toLowerCase() === data.files[0].name.toLowerCase()) {
fileExists = true;
return;
}
});

if (fileExists) {
//display the dialogs with the question to overwrite or not
data.context.find('.ss-uploadfield-item-status').text(config.errorMessages.overwriteWarning).css('max-width','75%');
data.context.find('.ss-uploadfield-item-progress').hide();
data.context.find('.ss-uploadfield-item-overwrite').show();
data.context.find('.ss-uploadfield-item-overwrite-warning').on('click', function(){
data.context.find('.ss-uploadfield-item-progress').show();
data.context.find('.ss-uploadfield-item-overwrite').hide();

//upload only if the "overwrite" button is clicked
$.blueimpUI.fileupload.prototype._onSend.call(that, e, data);
});
} else { //regular file upload
return $.blueimpUI.fileupload.prototype._onSend.call(that, e, data);
}
},
_onAlways: function (jqXHRorResult, textStatus, jqXHRorError, options) {
$.blueimpUI.fileupload.prototype._onAlways.call(this, jqXHRorResult, textStatus, jqXHRorError, options);
if (this._active === 0) {
Expand Down
2 changes: 2 additions & 0 deletions javascript/UploadField_uploadtemplate.js
Expand Up @@ -21,6 +21,8 @@ window.tmpl.cache['ss-uploadfield-uploadtemplate'] = tmpl(
'{% } %}' +
'{% } %}' +
'<div class="ss-uploadfield-item-cancel cancel"><button data-icon="deleteLight" class="ss-uploadfield-item-cancel" title="' + ss.i18n._t('UploadField.CANCELREMOVE', 'Cancel/Remove') + '">' + ss.i18n._t('UploadField.CANCEL', 'Cancel') + '</button></div>' +
'<div class="ss-uploadfield-item-overwrite hide ">'+
'<button data-icon="drive-upload" class="ss-uploadfield-item-overwrite-warning" title="' + ss.i18n._t('UploadField.OVERWRITE', 'Overwrite') + '">' + ss.i18n._t('UploadField.OVERWRITE', 'Overwrite') +
'</div>' +
'</div>' +
'</li>' +
Expand Down
3 changes: 2 additions & 1 deletion javascript/lang/de_DE.js
Expand Up @@ -34,6 +34,7 @@ if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
'UploadField.EMPTYRESULT': 'Leere Datei erhalten',
'UploadField.LOADING': 'Lädt ...',
'UploadField.Editing': 'Bearbeite ...',
'UploadField.Uploaded': 'Hochgeladen'
'UploadField.Uploaded': 'Hochgeladen',
'UploadField.OVERWRITEWARNING': 'Datei mit diesem Namen existiert bereits'
});
}
3 changes: 2 additions & 1 deletion javascript/lang/en_US.js
Expand Up @@ -35,6 +35,7 @@ if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
'UploadField.EMPTYRESULT': 'Empty file upload result',
'UploadField.LOADING': 'Loading ...',
'UploadField.Editing': 'Editing ...',
'UploadField.Uploaded': 'Uploaded'
'UploadField.Uploaded': 'Uploaded',
'UploadField.OVERWRITEWARNING': 'File with the same name already exists'
});
}
2 changes: 1 addition & 1 deletion scss/_elementMixins.scss
Expand Up @@ -29,7 +29,7 @@
// TODO tmp hack until we have permissions and can disable delete
display: none;
}
&.ss-uploadfield-item-cancel{
&.ss-uploadfield-item-cancel, &.ss-uploadfield-item-overwrite-warning {
@include border-radius(0);
border-left:1px solid rgba(#fff, 0.2);
margin-top:0px;
Expand Down

0 comments on commit 34bfc86

Please sign in to comment.