UNPKG

molstar

Version:

A comprehensive macromolecular library.

42 lines (41 loc) 1.63 kB
/** * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ // TODO only support compressed files for which uncompression support is available??? // TODO store globally with decompression plugins? var compressedExtList = ['gz', 'zip']; export function getFileInfo(file) { var path; var protocol = ''; if (file instanceof File) { path = file.name; } else if (file instanceof Blob) { path = ''; } else { path = file; } var queryIndex = path.lastIndexOf('?'); var query = queryIndex !== -1 ? path.substring(queryIndex) : ''; path = path.substring(0, queryIndex === -1 ? path.length : queryIndex); var name = path.replace(/^.*[\\/]/, ''); var base = name.substring(0, name.lastIndexOf('.')); var nameSplit = name.split('.'); var ext = nameSplit.length > 1 ? (nameSplit.pop() || '').toLowerCase() : ''; var protocolMatch = path.match(/^(.+):\/\/(.+)$/); if (protocolMatch) { protocol = protocolMatch[1].toLowerCase(); path = protocolMatch[2] || ''; } var dir = path.substring(0, path.lastIndexOf('/') + 1); if (compressedExtList.includes(ext)) { var n = path.length - ext.length - 1; ext = (path.substr(0, n).split('.').pop() || '').toLowerCase(); var m = base.length - ext.length - 1; base = base.substr(0, m); } return { path: path, name: name, ext: ext, base: base, dir: dir, protocol: protocol, query: query, src: file }; }