Commit f684cd89 authored by Martin Wortschack's avatar Martin Wortschack

Merge branch 'ps-step-1-add-mime-type-to-ide-file' into 'master'

Step 1 - Update IDE to track mimeType

See merge request gitlab-org/gitlab!50245
parents 7d9b2795 1aa2a240
...@@ -25,10 +25,10 @@ export default { ...@@ -25,10 +25,10 @@ export default {
}, },
methods: { methods: {
createFile(target, file) { createFile(target, file) {
const { name } = file; const { name, type: mimeType } = file;
const encodedContent = target.result.split('base64,')[1]; const encodedContent = target.result.split('base64,')[1];
const rawContent = encodedContent ? atob(encodedContent) : ''; const rawContent = encodedContent ? atob(encodedContent) : '';
const isText = isTextFile({ content: rawContent, mimeType: file.type, name }); const isText = isTextFile({ content: rawContent, mimeType, name });
const emitCreateEvent = content => const emitCreateEvent = content =>
this.$emit('create', { this.$emit('create', {
...@@ -36,6 +36,7 @@ export default { ...@@ -36,6 +36,7 @@ export default {
type: 'blob', type: 'blob',
content, content,
rawPath: !isText ? URL.createObjectURL(file) : '', rawPath: !isText ? URL.createObjectURL(file) : '',
mimeType,
}); });
if (isText) { if (isText) {
......
...@@ -11,8 +11,20 @@ export const splitParent = path => { ...@@ -11,8 +11,20 @@ export const splitParent = path => {
/** /**
* Create file objects from a list of file paths. * Create file objects from a list of file paths.
*
* @param {Array} options.data Array of blob paths to parse and create a file tree from.
* @param {Boolean} options.tempFile Web IDE flag for whether this is a "new" file or not.
* @param {String} options.content Content to initialize the new blob with.
* @param {String} options.rawPath Raw path used for the new blob.
* @param {Object} options.blobData Extra values to initialize each blob with.
*/ */
export const decorateFiles = ({ data, tempFile = false, content = '', rawPath = '' }) => { export const decorateFiles = ({
data,
tempFile = false,
content = '',
rawPath = '',
blobData = {},
}) => {
const treeList = []; const treeList = [];
const entries = {}; const entries = {};
...@@ -73,6 +85,7 @@ export const decorateFiles = ({ data, tempFile = false, content = '', rawPath = ...@@ -73,6 +85,7 @@ export const decorateFiles = ({ data, tempFile = false, content = '', rawPath =
content, content,
rawPath, rawPath,
parentPath, parentPath,
...blobData,
}); });
Object.assign(entries, { Object.assign(entries, {
......
...@@ -31,7 +31,7 @@ export const setResizingStatus = ({ commit }, resizing) => { ...@@ -31,7 +31,7 @@ export const setResizingStatus = ({ commit }, resizing) => {
export const createTempEntry = ( export const createTempEntry = (
{ state, commit, dispatch, getters }, { state, commit, dispatch, getters },
{ name, type, content = '', rawPath = '', openFile = true, makeFileActive = true }, { name, type, content = '', rawPath = '', openFile = true, makeFileActive = true, mimeType = '' },
) => { ) => {
const fullName = name.slice(-1) !== '/' && type === 'tree' ? `${name}/` : name; const fullName = name.slice(-1) !== '/' && type === 'tree' ? `${name}/` : name;
...@@ -56,6 +56,9 @@ export const createTempEntry = ( ...@@ -56,6 +56,9 @@ export const createTempEntry = (
tempFile: true, tempFile: true,
content, content,
rawPath, rawPath,
blobData: {
mimeType,
},
}); });
const { file, parentPath } = data; const { file, parentPath } = data;
......
...@@ -31,6 +31,7 @@ export const dataStructure = () => ({ ...@@ -31,6 +31,7 @@ export const dataStructure = () => ({
mrChange: null, mrChange: null,
deleted: false, deleted: false,
prevPath: undefined, prevPath: undefined,
mimeType: '',
}); });
export const decorateData = entity => { export const decorateData = entity => {
...@@ -47,6 +48,7 @@ export const decorateData = entity => { ...@@ -47,6 +48,7 @@ export const decorateData = entity => {
rawPath = '', rawPath = '',
file_lock, file_lock,
parentPath = '', parentPath = '',
mimeType = '',
} = entity; } = entity;
return Object.assign(dataStructure(), { return Object.assign(dataStructure(), {
...@@ -63,6 +65,7 @@ export const decorateData = entity => { ...@@ -63,6 +65,7 @@ export const decorateData = entity => {
rawPath, rawPath,
file_lock, file_lock,
parentPath, parentPath,
mimeType,
}); });
}; };
......
...@@ -62,8 +62,8 @@ describe('new dropdown upload', () => { ...@@ -62,8 +62,8 @@ describe('new dropdown upload', () => {
result: 'base64,8PDw8A==', // ðððð result: 'base64,8PDw8A==', // ðððð
}; };
const textFile = new File(['plain text'], 'textFile'); const textFile = new File(['plain text'], 'textFile', { type: 'test/mime-text' });
const binaryFile = new File(['😺'], 'binaryFile'); const binaryFile = new File(['😺'], 'binaryFile', { type: 'test/mime-binary' });
beforeEach(() => { beforeEach(() => {
jest.spyOn(FileReader.prototype, 'readAsText'); jest.spyOn(FileReader.prototype, 'readAsText');
...@@ -83,6 +83,7 @@ describe('new dropdown upload', () => { ...@@ -83,6 +83,7 @@ describe('new dropdown upload', () => {
type: 'blob', type: 'blob',
content: 'plain text', content: 'plain text',
rawPath: '', rawPath: '',
mimeType: 'test/mime-text',
}); });
}) })
.then(done) .then(done)
...@@ -99,6 +100,7 @@ describe('new dropdown upload', () => { ...@@ -99,6 +100,7 @@ describe('new dropdown upload', () => {
type: 'blob', type: 'blob',
content: 'ðððð', content: 'ðððð',
rawPath: 'blob:https://gitlab.com/048c7ac1-98de-4a37-ab1b-0206d0ea7e1b', rawPath: 'blob:https://gitlab.com/048c7ac1-98de-4a37-ab1b-0206d0ea7e1b',
mimeType: 'test/mime-binary',
}); });
}); });
}); });
......
import { decorateFiles, splitParent } from '~/ide/lib/files'; import { decorateFiles, splitParent } from '~/ide/lib/files';
import { decorateData } from '~/ide/stores/utils'; import { decorateData } from '~/ide/stores/utils';
const TEST_BLOB_DATA = { mimeType: 'test/mime' };
const createEntries = paths => { const createEntries = paths => {
const createEntry = (acc, { path, type, children }) => { const createEntry = (acc, { path, type, children }) => {
const { name, parent } = splitParent(path); const { name, parent } = splitParent(path);
...@@ -14,6 +16,7 @@ const createEntries = paths => { ...@@ -14,6 +16,7 @@ const createEntries = paths => {
parentPath: parent, parentPath: parent,
}), }),
tree: children.map(childName => expect.objectContaining({ name: childName })), tree: children.map(childName => expect.objectContaining({ name: childName })),
...(type === 'blob' ? TEST_BLOB_DATA : {}),
}; };
return acc; return acc;
...@@ -43,7 +46,7 @@ describe('IDE lib decorate files', () => { ...@@ -43,7 +46,7 @@ describe('IDE lib decorate files', () => {
{ path: 'README.md', type: 'blob', children: [] }, { path: 'README.md', type: 'blob', children: [] },
]); ]);
const { entries, treeList } = decorateFiles({ data }); const { entries, treeList } = decorateFiles({ data, blobData: TEST_BLOB_DATA });
// Here we test the keys and then each key/value individually because `expect(entries).toEqual(expectedEntries)` // Here we test the keys and then each key/value individually because `expect(entries).toEqual(expectedEntries)`
// was taking a very long time for some reason. Probably due to large objects and nested `expect.objectContaining`. // was taking a very long time for some reason. Probably due to large objects and nested `expect.objectContaining`.
......
...@@ -195,11 +195,13 @@ describe('Multi-file store actions', () => { ...@@ -195,11 +195,13 @@ describe('Multi-file store actions', () => {
.dispatch('createTempEntry', { .dispatch('createTempEntry', {
name, name,
type: 'blob', type: 'blob',
mimeType: 'test/mime',
}) })
.then(() => { .then(() => {
const f = store.state.entries[name]; const f = store.state.entries[name];
expect(f.tempFile).toBeTruthy(); expect(f.tempFile).toBeTruthy();
expect(f.mimeType).toBe('test/mime');
expect(store.state.trees['abcproject/mybranch'].tree.length).toBe(1); expect(store.state.trees['abcproject/mybranch'].tree.length).toBe(1);
done(); done();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment