pleroma-fe/src/services/file_type/file_type.service.js

37 lines
752 B
JavaScript
Raw Normal View History

2019-01-26 16:45:03 +01:00
// TODO this func might as well take the entire file and use its mimetype
// or the entire service could be just mimetype service that only operates
// on mimetypes and not files. Currently the naming is confusing.
const fileType = mimetype => {
if (mimetype.match(/flash/)) {
return 'flash'
}
2019-01-26 16:45:03 +01:00
if (mimetype.match(/text\/html/)) {
return 'html'
2016-11-25 18:21:25 +01:00
}
2019-01-26 16:45:03 +01:00
if (mimetype.match(/image/)) {
return 'image'
2016-11-25 18:21:25 +01:00
}
2019-01-26 16:45:03 +01:00
if (mimetype.match(/video/)) {
return 'video'
2016-11-25 18:21:25 +01:00
}
2019-01-26 16:45:03 +01:00
if (mimetype.match(/audio/)) {
return 'audio'
2016-11-25 18:21:25 +01:00
}
2019-01-26 16:45:03 +01:00
return 'unknown'
2016-11-25 18:21:25 +01:00
}
2019-01-26 16:45:03 +01:00
const fileMatchesSomeType = (types, file) =>
types.some(type => fileType(file.mimetype) === type)
2016-11-25 18:21:25 +01:00
const fileTypeService = {
2019-01-26 16:45:03 +01:00
fileType,
fileMatchesSomeType
2016-11-25 18:21:25 +01:00
}
export default fileTypeService