Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
d1fb29e5
Commit
d1fb29e5
authored
6 years ago
by
Jacques Erasmus
Committed by
Phil Hughes
6 years ago
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove base64 encoding from files that contain plain text
parent
114b0a3d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
36 deletions
+47
-36
app/assets/javascripts/ide/components/new_dropdown/upload.vue
...assets/javascripts/ide/components/new_dropdown/upload.vue
+25
-13
changelogs/unreleased/fix-base64-encoded-file-uploads.yml
changelogs/unreleased/fix-base64-encoded-file-uploads.yml
+5
-0
spec/javascripts/ide/components/new_dropdown/upload_spec.js
spec/javascripts/ide/components/new_dropdown/upload_spec.js
+17
-23
No files found.
app/assets/javascripts/ide/components/new_dropdown/upload.vue
View file @
d1fb29e5
...
@@ -25,14 +25,32 @@ export default {
...
@@ -25,14 +25,32 @@ export default {
},
},
},
},
methods
:
{
methods
:
{
createFile
(
target
,
file
,
isText
)
{
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
{
name
}
=
file
;
let
{
result
}
=
target
;
let
{
result
}
=
target
;
const
encodedContent
=
result
.
split
(
'
base64,
'
)[
1
];
const
rawContent
=
encodedContent
?
atob
(
encodedContent
)
:
''
;
const
isText
=
this
.
isText
(
rawContent
,
file
.
type
);
if
(
!
isText
)
{
result
=
isText
?
rawContent
:
encodedContent
;
// eslint-disable-next-line prefer-destructuring
result
=
result
.
split
(
'
base64,
'
)[
1
];
}
this
.
$emit
(
'
create
'
,
{
this
.
$emit
(
'
create
'
,
{
name
:
`
${
this
.
path
?
`
${
this
.
path
}
/`
:
''
}${
name
}
`
,
name
:
`
${
this
.
path
?
`
${
this
.
path
}
/`
:
''
}${
name
}
`
,
...
@@ -43,15 +61,9 @@ export default {
...
@@ -43,15 +61,9 @@ export default {
},
},
readFile
(
file
)
{
readFile
(
file
)
{
const
reader
=
new
FileReader
();
const
reader
=
new
FileReader
();
const
isText
=
file
.
type
.
match
(
/text.*/
)
!==
null
;
reader
.
addEventListener
(
'
load
'
,
e
=>
this
.
createFile
(
e
.
target
,
file
,
isText
),
{
once
:
true
});
reader
.
addEventListener
(
'
load
'
,
e
=>
this
.
createFile
(
e
.
target
,
file
),
{
once
:
true
});
reader
.
readAsDataURL
(
file
);
if
(
isText
)
{
reader
.
readAsText
(
file
);
}
else
{
reader
.
readAsDataURL
(
file
);
}
},
},
openFile
()
{
openFile
()
{
Array
.
from
(
this
.
$refs
.
fileUpload
.
files
).
forEach
(
file
=>
this
.
readFile
(
file
));
Array
.
from
(
this
.
$refs
.
fileUpload
.
files
).
forEach
(
file
=>
this
.
readFile
(
file
));
...
...
This diff is collapsed.
Click to expand it.
changelogs/unreleased/fix-base64-encoded-file-uploads.yml
0 → 100644
View file @
d1fb29e5
---
title
:
Remove base64 encoding from files that contain plain text
merge_request
:
22425
author
:
type
:
fixed
This diff is collapsed.
Click to expand it.
spec/javascripts/ide/components/new_dropdown/upload_spec.js
View file @
d1fb29e5
...
@@ -40,21 +40,10 @@ describe('new dropdown upload', () => {
...
@@ -40,21 +40,10 @@ describe('new dropdown upload', () => {
describe
(
'
readFile
'
,
()
=>
{
describe
(
'
readFile
'
,
()
=>
{
beforeEach
(()
=>
{
beforeEach
(()
=>
{
spyOn
(
FileReader
.
prototype
,
'
readAsText
'
);
spyOn
(
FileReader
.
prototype
,
'
readAsDataURL
'
);
spyOn
(
FileReader
.
prototype
,
'
readAsDataURL
'
);
});
});
it
(
'
calls readAsText for text files
'
,
()
=>
{
it
(
'
calls readAsDataURL for all files
'
,
()
=>
{
const
file
=
{
type
:
'
text/html
'
,
};
vm
.
readFile
(
file
);
expect
(
FileReader
.
prototype
.
readAsText
).
toHaveBeenCalledWith
(
file
);
});
it
(
'
calls readAsDataURL for non-text files
'
,
()
=>
{
const
file
=
{
const
file
=
{
type
:
'
images/png
'
,
type
:
'
images/png
'
,
};
};
...
@@ -66,32 +55,37 @@ describe('new dropdown upload', () => {
...
@@ -66,32 +55,37 @@ describe('new dropdown upload', () => {
});
});
describe
(
'
createFile
'
,
()
=>
{
describe
(
'
createFile
'
,
()
=>
{
const
target
=
{
const
t
extT
arget
=
{
result
:
'
content
'
,
result
:
'
base64,cGxhaW4gdGV4dA==
'
,
};
};
const
binaryTarget
=
{
const
binaryTarget
=
{
result
:
'
base64,base64content
'
,
result
:
'
base64,w4I=
'
,
};
const
textFile
=
{
name
:
'
textFile
'
,
type
:
'
text/plain
'
,
};
};
const
file
=
{
const
binaryFile
=
{
name
:
'
file
'
,
name
:
'
binaryFile
'
,
type
:
'
image/png
'
,
};
};
it
(
'
creates
new file
'
,
()
=>
{
it
(
'
creates
file in plain text (without encoding) if the file content is plain text
'
,
()
=>
{
vm
.
createFile
(
t
arget
,
file
,
tru
e
);
vm
.
createFile
(
t
extTarget
,
textFil
e
);
expect
(
vm
.
$emit
).
toHaveBeenCalledWith
(
'
create
'
,
{
expect
(
vm
.
$emit
).
toHaveBeenCalledWith
(
'
create
'
,
{
name
:
f
ile
.
name
,
name
:
textF
ile
.
name
,
type
:
'
blob
'
,
type
:
'
blob
'
,
content
:
target
.
result
,
content
:
'
plain text
'
,
base64
:
false
,
base64
:
false
,
});
});
});
});
it
(
'
splits content on base64 if binary
'
,
()
=>
{
it
(
'
splits content on base64 if binary
'
,
()
=>
{
vm
.
createFile
(
binaryTarget
,
file
,
fals
e
);
vm
.
createFile
(
binaryTarget
,
binaryFil
e
);
expect
(
vm
.
$emit
).
toHaveBeenCalledWith
(
'
create
'
,
{
expect
(
vm
.
$emit
).
toHaveBeenCalledWith
(
'
create
'
,
{
name
:
f
ile
.
name
,
name
:
binaryF
ile
.
name
,
type
:
'
blob
'
,
type
:
'
blob
'
,
content
:
binaryTarget
.
result
.
split
(
'
base64,
'
)[
1
],
content
:
binaryTarget
.
result
.
split
(
'
base64,
'
)[
1
],
base64
:
true
,
base64
:
true
,
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment