-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb4b8ce
commit cd7ee4d
Showing
6 changed files
with
40 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
import JSZip from 'jszip' | ||
import { unzipSync, strFromU8 } from 'fflate' | ||
|
||
/** | ||
* Reads XLSX file in a browser. | ||
* @param {file} file - A file being uploaded in the browser. | ||
* @param {File} file - A `File` object being uploaded in the browser. | ||
* @return {Promise} Resolves to an object holding XLSX file entries. | ||
*/ | ||
export default function unpackXlsxFile(file) { | ||
const files = {} | ||
|
||
return JSZip.loadAsync(file).then((zip) => { | ||
const files = [] | ||
zip.forEach((relativePath, zipEntry) => { | ||
if (!zipEntry.dir) { | ||
files.push(zipEntry.name) | ||
} | ||
}) | ||
|
||
const entries = {} | ||
return Promise.all(files.map((file) => { | ||
return zip.file(file).async('string').then(content => entries[file] = content) | ||
})) | ||
.then(() => entries) | ||
const startedAt = Date.now() | ||
return file.arrayBuffer().then((fileBuffer) => { | ||
const archive = new Uint8Array(fileBuffer) | ||
const contents = unzipSync(archive) | ||
return getContents(contents) | ||
// return new Promise((resolve, reject) => { | ||
// unzip(archive, (error, contents) => { | ||
// if (error) { | ||
// return reject(error) | ||
// } | ||
// return resolve(getContents(contents)) | ||
// }) | ||
// }) | ||
}) | ||
} | ||
|
||
function getContents(contents) { | ||
const unzippedFiles = [] | ||
for (const key of Object.keys(contents)) { | ||
unzippedFiles[key] = strFromU8(contents[key]) | ||
} | ||
return unzippedFiles | ||
} |