Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
mat2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tails
mat2
Commits
c186fc42
Commit
c186fc42
authored
6 years ago
by
Julien (jvoisin) Voisin
Browse files
Options
Downloads
Patches
Plain Diff
Clean deep metadata for zip files
parent
6d506b87
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/libreoffice.py
+11
-1
11 additions, 1 deletion
src/libreoffice.py
src/office.py
+13
-1
13 additions, 1 deletion
src/office.py
tests/test_libmat2.py
+14
-3
14 additions, 3 deletions
tests/test_libmat2.py
with
38 additions
and
5 deletions
src/libreoffice.py
+
11
−
1
View file @
c186fc42
...
...
@@ -34,6 +34,13 @@ class LibreOfficeParser(abstract.AbstractParser):
zipin
.
close
()
return
metadata
def
__clean_zipinfo
(
self
,
zipinfo
:
zipfile
.
ZipInfo
)
->
zipfile
.
ZipInfo
:
zipinfo
.
compress_type
=
zipfile
.
ZIP_DEFLATED
zipinfo
.
create_system
=
3
# Linux
zipinfo
.
comment
=
b
''
zipinfo
.
date_time
=
(
1980
,
1
,
1
,
0
,
0
,
0
)
return
zipinfo
def
remove_all
(
self
):
zin
=
zipfile
.
ZipFile
(
self
.
filename
,
'
r
'
)
zout
=
zipfile
.
ZipFile
(
self
.
output_filename
,
'
w
'
)
...
...
@@ -51,7 +58,10 @@ class LibreOfficeParser(abstract.AbstractParser):
print
(
"
%s isn
'
t supported
"
%
item
.
filename
)
continue
tmp_parser
.
remove_all
()
zout
.
write
(
tmp_parser
.
output_filename
,
item
.
filename
)
zinfo
=
zipfile
.
ZipInfo
(
item
.
filename
)
item
=
self
.
__clean_zipinfo
(
item
)
with
open
(
tmp_parser
.
output_filename
,
'
rb
'
)
as
f
:
zout
.
writestr
(
zinfo
,
f
.
read
())
shutil
.
rmtree
(
temp_folder
)
zout
.
close
()
zin
.
close
()
...
...
This diff is collapsed.
Click to expand it.
src/office.py
+
13
−
1
View file @
c186fc42
...
...
@@ -33,6 +33,13 @@ class OfficeParser(abstract.AbstractParser):
zipin
.
close
()
return
metadata
def
__clean_zipinfo
(
self
,
zipinfo
:
zipfile
.
ZipInfo
)
->
zipfile
.
ZipInfo
:
zipinfo
.
compress_type
=
zipfile
.
ZIP_DEFLATED
zipinfo
.
create_system
=
3
# Linux
zipinfo
.
comment
=
b
''
zipinfo
.
date_time
=
(
1980
,
1
,
1
,
0
,
0
,
0
)
return
zipinfo
def
remove_all
(
self
):
zin
=
zipfile
.
ZipFile
(
self
.
filename
,
'
r
'
)
zout
=
zipfile
.
ZipFile
(
self
.
output_filename
,
'
w
'
)
...
...
@@ -45,6 +52,7 @@ class OfficeParser(abstract.AbstractParser):
if
not
item
.
filename
.
endswith
(
'
.rels
'
):
continue
# don't keep metadata files
if
item
.
filename
in
self
.
files_to_keep
:
item
=
self
.
__clean_zipinfo
(
item
)
zout
.
writestr
(
item
,
zin
.
read
(
item
))
continue
...
...
@@ -54,7 +62,11 @@ class OfficeParser(abstract.AbstractParser):
print
(
"
%s isn
'
t supported
"
%
item
.
filename
)
continue
tmp_parser
.
remove_all
()
zout
.
write
(
tmp_parser
.
output_filename
,
item
.
filename
)
zinfo
=
zipfile
.
ZipInfo
(
item
.
filename
)
item
=
self
.
__clean_zipinfo
(
item
)
with
open
(
tmp_parser
.
output_filename
,
'
rb
'
)
as
f
:
zout
.
writestr
(
zinfo
,
f
.
read
())
shutil
.
rmtree
(
temp_folder
)
zout
.
close
()
zin
.
close
()
...
...
This diff is collapsed.
Click to expand it.
tests/test_libmat2.py
+
14
−
3
View file @
c186fc42
...
...
@@ -57,7 +57,7 @@ class TestGetMeta(unittest.TestCase):
class
TestDeepCleaning
(
unittest
.
TestCase
):
def
__check_
zip_clean
(
self
,
p
):
def
__check_
deep_meta
(
self
,
p
):
tempdir
=
tempfile
.
mkdtemp
()
zipin
=
zipfile
.
ZipFile
(
p
.
filename
)
zipin
.
extractall
(
tempdir
)
...
...
@@ -72,6 +72,15 @@ class TestDeepCleaning(unittest.TestCase):
self
.
assertEqual
(
inside_p
.
get_meta
(),
{})
shutil
.
rmtree
(
tempdir
)
def
__check_zip_meta
(
self
,
p
):
zipin
=
zipfile
.
ZipFile
(
p
.
filename
)
for
item
in
zipin
.
infolist
():
self
.
assertEqual
(
item
.
comment
,
b
''
)
self
.
assertEqual
(
item
.
date_time
,
(
1980
,
1
,
1
,
0
,
0
,
0
))
self
.
assertEqual
(
item
.
create_system
,
3
)
# 3 is UNIX
def
test_office
(
self
):
shutil
.
copy
(
'
./tests/data/dirty.docx
'
,
'
./tests/data/clean.docx
'
)
p
=
office
.
OfficeParser
(
'
./tests/data/clean.docx
'
)
...
...
@@ -85,7 +94,8 @@ class TestDeepCleaning(unittest.TestCase):
p
=
office
.
OfficeParser
(
'
./tests/data/clean.docx.cleaned
'
)
self
.
assertEqual
(
p
.
get_meta
(),
{})
self
.
__check_zip_clean
(
p
)
self
.
__check_zip_meta
(
p
)
self
.
__check_deep_meta
(
p
)
os
.
remove
(
'
./tests/data/clean.docx
'
)
...
...
@@ -103,7 +113,8 @@ class TestDeepCleaning(unittest.TestCase):
p
=
libreoffice
.
LibreOfficeParser
(
'
./tests/data/clean.odt.cleaned
'
)
self
.
assertEqual
(
p
.
get_meta
(),
{})
self
.
__check_zip_clean
(
p
)
self
.
__check_zip_meta
(
p
)
self
.
__check_deep_meta
(
p
)
os
.
remove
(
'
./tests/data/clean.odt
'
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment