Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
d1fb29e5
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
d1fb29e5
编写于
10月 22, 2018
作者:
J
Jacques Erasmus
提交者:
Phil Hughes
10月 22, 2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Remove base64 encoding from files that contain plain text
上级
114b0a3d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
47 addition
and
36 deletion
+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
未找到文件。
app/assets/javascripts/ide/components/new_dropdown/upload.vue
浏览文件 @
d1fb29e5
...
...
@@ -25,14 +25,32 @@ export default {
},
},
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
;
let
{
result
}
=
target
;
const
encodedContent
=
result
.
split
(
'
base64,
'
)[
1
];
const
rawContent
=
encodedContent
?
atob
(
encodedContent
)
:
''
;
const
isText
=
this
.
isText
(
rawContent
,
file
.
type
);
if
(
!
isText
)
{
// eslint-disable-next-line prefer-destructuring
result
=
result
.
split
(
'
base64,
'
)[
1
];
}
result
=
isText
?
rawContent
:
encodedContent
;
this
.
$emit
(
'
create
'
,
{
name
:
`
${
this
.
path
?
`
${
this
.
path
}
/`
:
''
}${
name
}
`
,
...
...
@@ -43,15 +61,9 @@ export default {
},
readFile
(
file
)
{
const
reader
=
new
FileReader
();
const
isText
=
file
.
type
.
match
(
/text.*/
)
!==
null
;
reader
.
addEventListener
(
'
load
'
,
e
=>
this
.
createFile
(
e
.
target
,
file
,
isText
),
{
once
:
true
});
if
(
isText
)
{
reader
.
readAsText
(
file
);
}
else
{
reader
.
readAsDataURL
(
file
);
}
reader
.
addEventListener
(
'
load
'
,
e
=>
this
.
createFile
(
e
.
target
,
file
),
{
once
:
true
});
reader
.
readAsDataURL
(
file
);
},
openFile
()
{
Array
.
from
(
this
.
$refs
.
fileUpload
.
files
).
forEach
(
file
=>
this
.
readFile
(
file
));
...
...
changelogs/unreleased/fix-base64-encoded-file-uploads.yml
0 → 100644
浏览文件 @
d1fb29e5
---
title
:
Remove base64 encoding from files that contain plain text
merge_request
:
22425
author
:
type
:
fixed
spec/javascripts/ide/components/new_dropdown/upload_spec.js
浏览文件 @
d1fb29e5
...
...
@@ -40,21 +40,10 @@ describe('new dropdown upload', () => {
describe
(
'
readFile
'
,
()
=>
{
beforeEach
(()
=>
{
spyOn
(
FileReader
.
prototype
,
'
readAsText
'
);
spyOn
(
FileReader
.
prototype
,
'
readAsDataURL
'
);
});
it
(
'
calls readAsText for text files
'
,
()
=>
{
const
file
=
{
type
:
'
text/html
'
,
};
vm
.
readFile
(
file
);
expect
(
FileReader
.
prototype
.
readAsText
).
toHaveBeenCalledWith
(
file
);
});
it
(
'
calls readAsDataURL for non-text files
'
,
()
=>
{
it
(
'
calls readAsDataURL for all files
'
,
()
=>
{
const
file
=
{
type
:
'
images/png
'
,
};
...
...
@@ -66,32 +55,37 @@ describe('new dropdown upload', () => {
});
describe
(
'
createFile
'
,
()
=>
{
const
target
=
{
result
:
'
content
'
,
const
t
extT
arget
=
{
result
:
'
base64,cGxhaW4gdGV4dA==
'
,
};
const
binaryTarget
=
{
result
:
'
base64,base64content
'
,
result
:
'
base64,w4I=
'
,
};
const
textFile
=
{
name
:
'
textFile
'
,
type
:
'
text/plain
'
,
};
const
file
=
{
name
:
'
file
'
,
const
binaryFile
=
{
name
:
'
binaryFile
'
,
type
:
'
image/png
'
,
};
it
(
'
creates
new file
'
,
()
=>
{
vm
.
createFile
(
t
arget
,
file
,
tru
e
);
it
(
'
creates
file in plain text (without encoding) if the file content is plain text
'
,
()
=>
{
vm
.
createFile
(
t
extTarget
,
textFil
e
);
expect
(
vm
.
$emit
).
toHaveBeenCalledWith
(
'
create
'
,
{
name
:
f
ile
.
name
,
name
:
textF
ile
.
name
,
type
:
'
blob
'
,
content
:
target
.
result
,
content
:
'
plain text
'
,
base64
:
false
,
});
});
it
(
'
splits content on base64 if binary
'
,
()
=>
{
vm
.
createFile
(
binaryTarget
,
file
,
fals
e
);
vm
.
createFile
(
binaryTarget
,
binaryFil
e
);
expect
(
vm
.
$emit
).
toHaveBeenCalledWith
(
'
create
'
,
{
name
:
f
ile
.
name
,
name
:
binaryF
ile
.
name
,
type
:
'
blob
'
,
content
:
binaryTarget
.
result
.
split
(
'
base64,
'
)[
1
],
base64
:
true
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录