Skip to content

Commit 5b13bde

Browse files
committed
fix: drop directory on webkit browsers
1 parent 7bb83cb commit 5b13bde

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/resumable.js

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@
110110
this.onDrop = function (event) {
111111
event.stopPropagation();
112112
event.preventDefault();
113-
$.addFiles(event.dataTransfer.files, event);
113+
var dataTransfer = event.dataTransfer;
114+
if (dataTransfer.items && dataTransfer.items[0].webkitGetAsEntry) {
115+
$.webkitReadDataTransfer(event);
116+
} else {
117+
$.addFiles(dataTransfer.files, event);
118+
}
114119
};
115120

116121
/**
@@ -169,6 +174,45 @@
169174
return !preventDefault;
170175
},
171176

177+
/**
178+
* Read webkit dataTransfer object
179+
* @param event
180+
*/
181+
webkitReadDataTransfer: function (event) {
182+
var $ = this;
183+
each(event.dataTransfer.items, function (item) {
184+
var entry = item.webkitGetAsEntry();
185+
if (!entry) {
186+
return ;
187+
}
188+
if (entry.isFile) {
189+
// due to a bug in Chrome's File System API impl - #149735
190+
fileReadSuccess(item.getAsFile(), entry.fullPath);
191+
} else {
192+
entry.createReader().readEntries(readSuccess, readError);
193+
}
194+
});
195+
function readSuccess(entries) {
196+
each(entries, function(entry) {
197+
if (entry.isFile) {
198+
var fullPath = entry.fullPath;
199+
entry.file(function (file) {
200+
fileReadSuccess(file, fullPath);
201+
}, readError);
202+
} else if (entry.isDirectory) {
203+
entry.createReader().readEntries(readSuccess, readError);
204+
}
205+
});
206+
}
207+
function fileReadSuccess(file, fullPath) {
208+
// relative path should not start with "/"
209+
file.relativePath = fullPath.substring(1);
210+
$.addFile(file, event);
211+
}
212+
function readError(fileError) {
213+
throw fileError;
214+
}
215+
},
172216

173217
/**
174218
* Generate unique identifier for a file
@@ -182,9 +226,8 @@
182226
return custom(file);
183227
}
184228
// Some confusion in different versions of Firefox
185-
var relativePath = file.webkitRelativePath || file.fileName || file.name;
186-
var size = file.size;
187-
return size + '-' + relativePath.replace(/[^0-9a-zA-Z_-]/img, '');
229+
var relativePath = file.relativePath || file.webkitRelativePath || file.fileName || file.name;
230+
return file.size + '-' + relativePath.replace(/[^0-9a-zA-Z_-]/img, '');
188231
},
189232

190233
/**
@@ -557,7 +600,7 @@
557600
* Relative file path
558601
* @type {string}
559602
*/
560-
this.relativePath = file.webkitRelativePath || this.name;
603+
this.relativePath = file.relativePath || file.webkitRelativePath || this.name;
561604

562605
/**
563606
* File unique identifier

0 commit comments

Comments
 (0)