Skip to content

Commit

Permalink
Remove mime-types/mime-db dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
quarklemotion committed May 17, 2018
1 parent 9928152 commit e0299f3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
6 changes: 4 additions & 2 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": false,
"name": "html5-file-selector",
"version": "1.0.2",
"version": "2.0.0",
"description": "A wrapper library for more easily handling html5 drag/drop and file input file and folder selections",
"homepage": "https://www.github.com/quarklemotion/html5-file-selector",
"repository": "quarklemotion/html5-file-selector",
Expand All @@ -27,8 +27,7 @@
"extends": "airbnb-base"
},
"dependencies": {
"babel-runtime": "6.11.x",
"mime-types": "2.1.x"
"babel-runtime": "6.11.x"
},
"devDependencies": {
"babel-cli": "6.16.x",
Expand Down
33 changes: 27 additions & 6 deletions src/Html5FileSelector.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import mimeTypes from 'mime-types';

const DEFAULT_FILES_TO_IGNORE = [
'.DS_Store', // OSX indexing file
'Thumbs.db' // Windows indexing file
];

// map of common (mostly media types) mime types to use when the browser does not supply the mime type
const EXTENSION_TO_MIME_TYPE_MAP = {
avi: 'video/avi',
gif: 'image/gif',
ico: 'image/x-icon',
jpeg: 'image/jpeg',
jpg: 'image/jpeg',
mkv: 'video/x-matroska',
mov: 'video/quicktime',
mp4: 'video/mp4',
pdf: 'application/pdf',
png: 'image/png',
zip: 'application/zip'
};

function shouldIgnoreFile(file) {
return DEFAULT_FILES_TO_IGNORE.indexOf(file.name) >= 0;
}

function copyString(aString) {
return ` ${aString}`.slice(1);
}

function traverseDirectory(entry) {
const reader = entry.createReader();
// Resolved when the entire directory is traversed
Expand Down Expand Up @@ -48,14 +65,18 @@ function packageFile(file, entry) {
// handle some browsers sometimes missing mime types for dropped files
const hasExtension = file.name && file.name.lastIndexOf('.') !== -1;
if (hasExtension && !file.type) {
fileTypeOverride = mimeTypes.lookup(file.name);
const fileExtension = (file.name || '').split('.').pop();
fileTypeOverride = EXTENSION_TO_MIME_TYPE_MAP[fileExtension];
}
return {
fileObject: file,
type: file.type ? file.type : fileTypeOverride,
fileObject: file, // provide access to the raw File object (required for uploading)
fullPath: entry ? copyString(entry.fullPath) : file.name,
lastModified: file.lastModified,
lastModifiedDate: file.lastModifiedDate,
name: file.name,
size: file.size,
fullPath: entry ? entry.fullPath : file.name
type: file.type ? file.type : fileTypeOverride,
webkitRelativePath: file.webkitRelativePath
};
}

Expand Down

0 comments on commit e0299f3

Please sign in to comment.