Skip to content
Snippets Groups Projects

Remove base64 encoding from files that contain plain text

Merged Jacques Erasmus requested to merge fix-base64-encoded-file-uploads into master
All threads resolved!
Files
3
@@ -26,29 +26,28 @@ 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) {
if (isKnownBinaryFileType) {
return false;
}
if(isKnownTextFileType) {
if (isKnownTextFileType) {
return true;
}
// if it's not a known file type, try to determine the type by evaluating the file contents
// 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;
let { result } = target;
const encodedContent = result.split('base64,')[1];
const rawContent = atob(encodedContent);
const rawContent = encodedContent ? atob(encodedContent) : '';
const isText = this.isText(rawContent, file.type);
result = isText ? rawContent : encodedContent;
Loading