|
165 | 165 | bitrateInterval: 500, |
166 | 166 | // By default, uploads are started automatically when adding files: |
167 | 167 | autoUpload: true, |
| 168 | + // By default, duplicate file names are expected to be handled on |
| 169 | + // the server-side. If this is not possible (e.g. when uploading |
| 170 | + // files directly to Amazon S3), the following option can be set to |
| 171 | + // an empty object or an object mapping existing filenames, e.g.: |
| 172 | + // { "image.jpg": true, "image (1).jpg": true } |
| 173 | + // If it is set, all files will be uploaded with unique filenames, |
| 174 | + // adding increasing number suffixes if necessary, e.g.: |
| 175 | + // "image (2).jpg" |
| 176 | + uniqueFilenames: undefined, |
168 | 177 |
|
169 | 178 | // Error and info messages: |
170 | 179 | messages: { |
|
449 | 458 | return Object.prototype.toString.call(obj) === '[object ' + type + ']'; |
450 | 459 | }, |
451 | 460 |
|
| 461 | + _getUniqueFilename: function (name, map) { |
| 462 | + name = String(name); |
| 463 | + if (map[name]) { |
| 464 | + name = name.replace( |
| 465 | + /(?: \(([\d]+)\))?(\.[^.]+)?$/, |
| 466 | + function (_, p1, p2) { |
| 467 | + var index = p1 ? Number(p1) + 1 : 1; |
| 468 | + var ext = p2 || ''; |
| 469 | + return ' (' + index + ')' + ext; |
| 470 | + } |
| 471 | + ); |
| 472 | + return this._getUniqueFilename(name, map); |
| 473 | + } |
| 474 | + map[name] = true; |
| 475 | + return name; |
| 476 | + }, |
| 477 | + |
452 | 478 | _initXHRData: function (options) { |
453 | 479 | var that = this, |
454 | 480 | formData, |
|
510 | 536 | // dummy objects: |
511 | 537 | if (that._isInstanceOf('File', file) || |
512 | 538 | that._isInstanceOf('Blob', file)) { |
| 539 | + var fileName = file.uploadName || file.name; |
| 540 | + if (options.uniqueFilenames) { |
| 541 | + fileName = that._getUniqueFilename( |
| 542 | + fileName, |
| 543 | + options.uniqueFilenames |
| 544 | + ); |
| 545 | + } |
513 | 546 | formData.append( |
514 | 547 | ($.type(options.paramName) === 'array' && |
515 | 548 | options.paramName[index]) || paramName, |
516 | 549 | file, |
517 | | - file.uploadName || file.name |
| 550 | + fileName |
518 | 551 | ); |
519 | 552 | } |
520 | 553 | }); |
|
0 commit comments