Commit 89256d4d authored by Maciej Krüger's avatar Maciej Krüger

Fix WebIDE image being base64

Problem was createCommitPayload expects a blob url but we gave a data
url and base64 content

This fixes it and also makes the preview work again

Changelog: fixed
MR: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/65165
parent 09d4ec74
...@@ -415,7 +415,11 @@ export default { ...@@ -415,7 +415,11 @@ export default {
const parentPath = getPathParent(this.file.path); const parentPath = getPathParent(this.file.path);
const path = `${parentPath ? `${parentPath}/` : ''}${file.name}`; const path = `${parentPath ? `${parentPath}/` : ''}${file.name}`;
return this.addTempImage({ name: path, rawPath: content }).then(({ name: fileName }) => { return this.addTempImage({
name: path,
rawPath: URL.createObjectURL(file),
content: atob(content.split('base64,')[1]),
}).then(({ name: fileName }) => {
this.editor.replaceSelectedText(`![${fileName}](./${fileName})`); this.editor.replaceSelectedText(`![${fileName}](./${fileName})`);
}); });
}); });
......
...@@ -76,11 +76,11 @@ export const createTempEntry = ( ...@@ -76,11 +76,11 @@ export const createTempEntry = (
return file; return file;
}; };
export const addTempImage = ({ dispatch, getters }, { name, rawPath = '' }) => export const addTempImage = ({ dispatch, getters }, { name, rawPath = '', content = '' }) =>
dispatch('createTempEntry', { dispatch('createTempEntry', {
name: getters.getAvailableFileName(name), name: getters.getAvailableFileName(name),
type: 'blob', type: 'blob',
content: rawPath.split('base64,')[1], content,
rawPath, rawPath,
openFile: false, openFile: false,
makeFileActive: false, makeFileActive: false,
......
...@@ -252,10 +252,10 @@ export function extractMarkdownImagesFromEntries(mdFile, entries) { ...@@ -252,10 +252,10 @@ export function extractMarkdownImagesFromEntries(mdFile, entries) {
.trim(); .trim();
const imageContent = entries[imagePath]?.content || entries[imagePath]?.raw; const imageContent = entries[imagePath]?.content || entries[imagePath]?.raw;
const imageRawPath = entries[imagePath]?.rawPath;
if (!isAbsolute(path) && imageContent) { if (!isAbsolute(path) && imageContent) {
const ext = path.includes('.') ? path.split('.').pop().trim() : 'jpeg'; const src = imageRawPath;
const src = `data:image/${ext};base64,${imageContent}`;
i += 1; i += 1;
const key = `{{${prefix}${i}}}`; const key = `{{${prefix}${i}}}`;
images[key] = { alt, src, title }; images[key] = { alt, src, title };
......
...@@ -640,11 +640,12 @@ describe('RepoEditor', () => { ...@@ -640,11 +640,12 @@ describe('RepoEditor', () => {
pasteImage(); pasteImage();
await waitForFileContentChange(); await waitForFileContentChange();
expect(vm.$store.state.entries['foo/foo.png'].rawPath.startsWith('blob:')).toBe(true);
expect(vm.$store.state.entries['foo/foo.png']).toMatchObject({ expect(vm.$store.state.entries['foo/foo.png']).toMatchObject({
path: 'foo/foo.png', path: 'foo/foo.png',
type: 'blob', type: 'blob',
content: 'Zm9v', content: 'foo',
rawPath: 'data:image/png;base64,Zm9v', rawPath: vm.$store.state.entries['foo/foo.png'].rawPath,
}); });
}); });
......
...@@ -604,7 +604,7 @@ describe('Multi-file store utils', () => { ...@@ -604,7 +604,7 @@ describe('Multi-file store utils', () => {
let entries; let entries;
beforeEach(() => { beforeEach(() => {
const img = { content: '/base64/encoded/image+' }; const img = { content: 'png-gibberish', rawPath: 'blob:1234' };
mdFile = { path: 'path/to/some/directory/myfile.md' }; mdFile = { path: 'path/to/some/directory/myfile.md' };
entries = { entries = {
// invalid (or lack of) extensions are also supported as long as there's // invalid (or lack of) extensions are also supported as long as there's
...@@ -637,14 +637,14 @@ describe('Multi-file store utils', () => { ...@@ -637,14 +637,14 @@ describe('Multi-file store utils', () => {
${'* ![img](img.png "title here")'} | ${'png'} | ${'img'} | ${'title here'} ${'* ![img](img.png "title here")'} | ${'png'} | ${'img'} | ${'title here'}
`( `(
'correctly transforms markdown with uncommitted images: $markdownBefore', 'correctly transforms markdown with uncommitted images: $markdownBefore',
({ markdownBefore, ext, imgAlt, imgTitle }) => { ({ markdownBefore, imgAlt, imgTitle }) => {
mdFile.content = markdownBefore; mdFile.content = markdownBefore;
expect(utils.extractMarkdownImagesFromEntries(mdFile, entries)).toEqual({ expect(utils.extractMarkdownImagesFromEntries(mdFile, entries)).toEqual({
content: '* {{gl_md_img_1}}', content: '* {{gl_md_img_1}}',
images: { images: {
'{{gl_md_img_1}}': { '{{gl_md_img_1}}': {
src: `data:image/${ext};base64,/base64/encoded/image+`, src: 'blob:1234',
alt: imgAlt, alt: imgAlt,
title: imgTitle, title: imgTitle,
}, },
......
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