diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-11 21:09:19 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-03-11 21:09:19 +0000 |
commit | fca89bb73ff5b1d14c98c72481f9268fee107ea0 (patch) | |
tree | e1c8a2c4fe5df7f054fd09e49f53bcfb51e51c84 /app/assets/javascripts/ide | |
parent | 76e9fc7b29c1ce716c26932e9fbec0f3c99f53f4 (diff) | |
download | gitlab-ce-fca89bb73ff5b1d14c98c72481f9268fee107ea0.tar.gz |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/assets/javascripts/ide')
-rw-r--r-- | app/assets/javascripts/ide/components/new_dropdown/upload.vue | 21 | ||||
-rw-r--r-- | app/assets/javascripts/ide/utils.js | 53 |
2 files changed, 55 insertions, 19 deletions
diff --git a/app/assets/javascripts/ide/components/new_dropdown/upload.vue b/app/assets/javascripts/ide/components/new_dropdown/upload.vue index 0efb0012246..7261e0590c8 100644 --- a/app/assets/javascripts/ide/components/new_dropdown/upload.vue +++ b/app/assets/javascripts/ide/components/new_dropdown/upload.vue @@ -1,5 +1,6 @@ <script> import ItemButton from './button.vue'; +import { isTextFile } from '~/ide/utils'; export default { components: { @@ -23,29 +24,11 @@ export default { }, }, methods: { - isText(content, fileType) { - const knownBinaryFileTypes = ['image/']; - const knownTextFileTypes = ['text/']; - const isKnownBinaryFileType = knownBinaryFileTypes.find(type => fileType.includes(type)); - const isKnownTextFileType = knownTextFileTypes.find(type => fileType.includes(type)); - const asciiRegex = /^[ -~\t\n\r]+$/; // tests whether a string contains ascii characters only (ranges from space to tilde, tabs and new lines) - - if (isKnownBinaryFileType) { - return false; - } - - if (isKnownTextFileType) { - return true; - } - - // if it's not a known file type, determine the type by evaluating the file contents - return asciiRegex.test(content); - }, createFile(target, file) { const { name } = file; const encodedContent = target.result.split('base64,')[1]; const rawContent = encodedContent ? atob(encodedContent) : ''; - const isText = this.isText(rawContent, file.type); + const isText = isTextFile(rawContent, file.type, name); const emitCreateEvent = content => this.$emit('create', { diff --git a/app/assets/javascripts/ide/utils.js b/app/assets/javascripts/ide/utils.js index ae579fef25f..64ac539a4ff 100644 --- a/app/assets/javascripts/ide/utils.js +++ b/app/assets/javascripts/ide/utils.js @@ -1,4 +1,57 @@ import { commitItemIconMap } from './constants'; +import { languages } from 'monaco-editor'; +import { flatten } from 'lodash'; + +const toLowerCase = x => x.toLowerCase(); + +const monacoLanguages = languages.getLanguages(); +const monacoExtensions = new Set( + flatten(monacoLanguages.map(lang => lang.extensions?.map(toLowerCase) || [])), +); +const monacoMimetypes = new Set( + flatten(monacoLanguages.map(lang => lang.mimetypes?.map(toLowerCase) || [])), +); +const monacoFilenames = new Set( + flatten(monacoLanguages.map(lang => lang.filenames?.map(toLowerCase) || [])), +); + +const KNOWN_TYPES = [ + { + isText: false, + isMatch(mimeType) { + return mimeType.toLowerCase().includes('image/'); + }, + }, + { + isText: true, + isMatch(mimeType) { + return mimeType.toLowerCase().includes('text/'); + }, + }, + { + isText: true, + isMatch(mimeType, fileName) { + const fileExtension = fileName.includes('.') ? `.${fileName.split('.').pop()}` : ''; + + return ( + monacoExtensions.has(fileExtension.toLowerCase()) || + monacoMimetypes.has(mimeType.toLowerCase()) || + monacoFilenames.has(fileName.toLowerCase()) + ); + }, + }, +]; + +export function isTextFile(content, mimeType, fileName) { + const knownType = KNOWN_TYPES.find(type => type.isMatch(mimeType, fileName)); + + if (knownType) return knownType.isText; + + // does the string contain ascii characters only (ranges from space to tilde, tabs and new lines) + const asciiRegex = /^[ -~\t\n\r]+$/; + // for unknown types, determine the type by evaluating the file contents + return asciiRegex.test(content); +} export const getCommitIconMap = file => { if (file.deleted) { |