Commit 02f5ed95 authored by Paul Slaughter's avatar Paul Slaughter

Update IDE to track mimeType

- Add mimeType to file object
- Add mimeType handling to upload file
parent e10ab985
......@@ -36,6 +36,7 @@ export default {
type: 'blob',
content,
rawPath: !isText ? URL.createObjectURL(file) : '',
mimeType: file.type,
});
if (isText) {
......
......@@ -11,8 +11,16 @@ export const splitParent = path => {
/**
* Create file objects from a list of file paths.
*
* @param options.blobData 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 entries = {};
......@@ -73,6 +81,7 @@ export const decorateFiles = ({ data, tempFile = false, content = '', rawPath =
content,
rawPath,
parentPath,
...blobData,
});
Object.assign(entries, {
......
......@@ -31,7 +31,7 @@ export const setResizingStatus = ({ commit }, resizing) => {
export const createTempEntry = (
{ 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;
......@@ -56,6 +56,9 @@ export const createTempEntry = (
tempFile: true,
content,
rawPath,
blobData: {
mimeType,
},
});
const { file, parentPath } = data;
......
......@@ -31,6 +31,7 @@ export const dataStructure = () => ({
mrChange: null,
deleted: false,
prevPath: undefined,
mimeType: '',
});
export const decorateData = entity => {
......@@ -47,6 +48,7 @@ export const decorateData = entity => {
rawPath = '',
file_lock,
parentPath = '',
mimeType = '',
} = entity;
return Object.assign(dataStructure(), {
......@@ -63,6 +65,7 @@ export const decorateData = entity => {
rawPath,
file_lock,
parentPath,
mimeType,
});
};
......
......@@ -62,8 +62,8 @@ describe('new dropdown upload', () => {
result: 'base64,8PDw8A==', // ðððð
};
const textFile = new File(['plain text'], 'textFile');
const binaryFile = new File(['😺'], 'binaryFile');
const textFile = new File(['plain text'], 'textFile', { type: 'test/mime-text' });
const binaryFile = new File(['😺'], 'binaryFile', { type: 'test/mime-binary' });
beforeEach(() => {
jest.spyOn(FileReader.prototype, 'readAsText');
......@@ -83,6 +83,7 @@ describe('new dropdown upload', () => {
type: 'blob',
content: 'plain text',
rawPath: '',
mimeType: 'test/mime-text',
});
})
.then(done)
......@@ -99,6 +100,7 @@ describe('new dropdown upload', () => {
type: 'blob',
content: 'ðððð',
rawPath: 'blob:https://gitlab.com/048c7ac1-98de-4a37-ab1b-0206d0ea7e1b',
mimeType: 'test/mime-binary',
});
});
});
......
import { decorateFiles, splitParent } from '~/ide/lib/files';
import { decorateData } from '~/ide/stores/utils';
const TEST_BLOB_DATA = { mimeType: 'test/mime' };
const createEntries = paths => {
const createEntry = (acc, { path, type, children }) => {
const { name, parent } = splitParent(path);
......@@ -14,6 +16,7 @@ const createEntries = paths => {
parentPath: parent,
}),
tree: children.map(childName => expect.objectContaining({ name: childName })),
...(type === 'blob' ? TEST_BLOB_DATA : {}),
};
return acc;
......@@ -43,7 +46,7 @@ describe('IDE lib decorate files', () => {
{ 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)`
// 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', () => {
.dispatch('createTempEntry', {
name,
type: 'blob',
mimeType: 'test/mime',
})
.then(() => {
const f = store.state.entries[name];
expect(f.tempFile).toBeTruthy();
expect(f.mimeType).toBe('test/mime');
expect(store.state.trees['abcproject/mybranch'].tree.length).toBe(1);
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