MOBILE-3320 lint: Fix linting in mimetype service

main
Noel De Martin 2020-10-08 12:36:16 +02:00
parent f4681c1c64
commit 0ca1a37b1b
4 changed files with 2425 additions and 35 deletions

View File

@ -56,7 +56,7 @@ module.exports = {
accessibility: 'no-public', accessibility: 'no-public',
}, },
], ],
'@typescript-eslint/indent': 'error', '@typescript-eslint/indent': 'off',
'@typescript-eslint/member-delimiter-style': [ '@typescript-eslint/member-delimiter-style': [
'error', 'error',
{ {
@ -169,7 +169,6 @@ module.exports = {
'error', 'error',
'unix', 'unix',
], ],
'lines-between-class-members': ['error', 'always'],
'max-len': [ 'max-len': [
'error', 'error',
{ {

View File

@ -13,36 +13,54 @@
// limitations under the License. // limitations under the License.
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { FileEntry } from '@ionic-native/file';
import { CoreFile } from '@services/file'; import { CoreFile } from '@services/file';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { makeSingleton, Translate, Http } from '@singletons/core.singletons'; import { makeSingleton, Translate, Http } from '@singletons/core.singletons';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { CoreWSExternalFile } from '@services/ws';
interface MimeTypeInfo {
type: string;
icon?: string;
groups?: string[];
// eslint-disable-next-line id-blacklist
string?: string;
}
interface MimeTypeGroupInfo {
mimetypes: string[];
extensions: string[];
}
const EXTENSION_REGEX = /^[a-z0-9]+$/;
/* /*
* "Utils" service with helper functions for mimetypes and extensions. * "Utils" service with helper functions for mimetypes and extensions.
*/ */
@Injectable() @Injectable()
export class CoreMimetypeUtilsProvider { export class CoreMimetypeUtilsProvider {
protected logger: CoreLogger; protected logger: CoreLogger;
protected extToMime = {}; // Object to map extensions -> mimetypes. protected extToMime: Record<string, MimeTypeInfo> = {};
protected mimeToExt = {}; // Object to map mimetypes -> extensions. protected mimeToExt: Record<string, string[]> = {};
protected groupsMimeInfo = {}; // Object to hold extensions and mimetypes that belong to a certain "group" (audio, video, ...). protected groupsMimeInfo: Record<string, MimeTypeGroupInfo> = {};
protected extensionRegex = /^[a-z0-9]+$/;
constructor() { constructor() {
this.logger = CoreLogger.getInstance('CoreMimetypeUtilsProvider'); this.logger = CoreLogger.getInstance('CoreMimetypeUtilsProvider');
Http.instance.get('assets/exttomime.json').subscribe((result) => { Http.instance.get('assets/exttomime.json').subscribe((result: Record<string, MimeTypeInfo>) => {
this.extToMime = result; this.extToMime = result;
}, (err) => { }, () => {
// Error, shouldn't happen. // Error, shouldn't happen.
}); });
Http.instance.get('assets/mimetoext.json').subscribe((result) => { Http.instance.get('assets/mimetoext.json').subscribe((result: Record<string, string[]>) => {
this.mimeToExt = result; this.mimeToExt = result;
}, (err) => { }, () => {
// Error, shouldn't happen. // Error, shouldn't happen
}); });
} }
@ -148,31 +166,36 @@ export class CoreMimetypeUtilsProvider {
* Set the embed type to display an embedded file and mimetype if not found. * Set the embed type to display an embedded file and mimetype if not found.
* *
* @param file File object. * @param file File object.
* @paran path Alternative path that will override fileurl from file object. * @param path Alternative path that will override fileurl from file object.
*/ */
getEmbeddedHtml(file: any, path?: string): string { getEmbeddedHtml(file: CoreWSExternalFile | FileEntry, path?: string): string {
let ext; const filename = 'isFile' in file ? (file as FileEntry).name : file.filename;
const filename = file.filename || file.name; const extension = !('isFile' in file) && file.mimetype ? this.getExtension(file.mimetype) : this.getFileExtension(filename);
const mimeType = !('isFile' in file) && file.mimetype ? file.mimetype : this.getMimeType(extension);
if (file.mimetype) { // @todo linting: See if this can be removed
ext = this.getExtension(file.mimetype); (file as CoreWSExternalFile).mimetype = mimeType;
} else {
ext = this.getFileExtension(filename);
file.mimetype = this.getMimeType(ext);
}
if (this.canBeEmbedded(ext)) { if (this.canBeEmbedded(extension)) {
file.embedType = this.getExtensionType(ext); const embedType = this.getExtensionType(extension);
path = CoreFile.instance.convertFileSrc(path || file.fileurl || file.url || (file.toURL && file.toURL())); // @todo linting: See if this can be removed
(file as { embedType: string }).embedType = embedType;
if (file.embedType == 'image') { path = CoreFile.instance.convertFileSrc(path ?? ('isFile' in file ? file.toURL() : file.fileurl));
return '<img src="' + path + '">';
} switch (embedType) {
if (file.embedType == 'audio' || file.embedType == 'video') { case 'image':
return '<' + file.embedType + ' controls title="' + filename + '" src="' + path + '">' + return `<img src="${path}">`;
'<source src="' + path + '" type="' + file.mimetype + '">' + case 'audio':
'</' + file.embedType + '>'; case 'video':
return [
`<${embedType} controls title="${filename}" src="${path}">`,
`<source src="${path}" type="${mimeType}">`,
`</${embedType}>`,
].join('');
default:
return '';
} }
} }
@ -290,7 +313,7 @@ export class CoreMimetypeUtilsProvider {
candidate = candidate.substr(0, position); candidate = candidate.substr(0, position);
} }
if (this.extensionRegex.test(candidate)) { if (EXTENSION_REGEX.test(candidate)) {
extension = candidate; extension = candidate;
} }
} }
@ -338,7 +361,7 @@ export class CoreMimetypeUtilsProvider {
* @param field The field to get. If not supplied, all the info will be returned. * @param field The field to get. If not supplied, all the info will be returned.
* @return Info for the group. * @return Info for the group.
*/ */
getGroupMimeInfo(group: string, field?: string): any { getGroupMimeInfo(group: string, field?: string): MimeTypeGroupInfo {
if (typeof this.groupsMimeInfo[group] == 'undefined') { if (typeof this.groupsMimeInfo[group] == 'undefined') {
this.fillGroupMimeInfo(group); this.fillGroupMimeInfo(group);
} }
@ -372,13 +395,13 @@ export class CoreMimetypeUtilsProvider {
* @param capitalise If true, capitalises first character of result. * @param capitalise If true, capitalises first character of result.
* @return Type description. * @return Type description.
*/ */
getMimetypeDescription(obj: any, capitalise?: boolean): string { getMimetypeDescription(obj: FileEntry | { filename: string; mimetype: string } | string, capitalise?: boolean): string {
const langPrefix = 'assets.mimetypes.'; const langPrefix = 'assets.mimetypes.';
let filename = ''; let filename = '';
let mimetype = ''; let mimetype = '';
let extension = ''; let extension = '';
if (typeof obj == 'object' && typeof obj.file == 'function') { if (typeof obj == 'object' && 'isFile' in obj) {
// It's a FileEntry. Don't use the file function because it's asynchronous and the type isn't reliable. // It's a FileEntry. Don't use the file function because it's asynchronous and the type isn't reliable.
filename = obj.name; filename = obj.name;
} else if (typeof obj == 'object') { } else if (typeof obj == 'object') {
@ -548,6 +571,7 @@ export class CoreMimetypeUtilsProvider {
return path; return path;
} }
} }
export class CoreMimetypeUtils extends makeSingleton(CoreMimetypeUtilsProvider) {} export class CoreMimetypeUtils extends makeSingleton(CoreMimetypeUtilsProvider) {}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff