forked from collective/collective.documentviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_cleanup_file_storage.py
87 lines (71 loc) · 2.72 KB
/
_cleanup_file_storage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from AccessControl.SecurityManagement import newSecurityManager
from collective.documentviewer.settings import GlobalSettings
from collective.documentviewer.settings import Settings
from collective.documentviewer.storage import getResourceDirectory
from plone.app.uuid.utils import uuidToObject
from zope.component.hooks import setSite
import argparse
import os
import shutil
parser = argparse.ArgumentParser(
description='Generate Gruntfile.js from Plone site')
parser.add_argument('--site-id', dest='siteid', default='Plone',
help='Some scripts care about site id')
parser.add_argument('--commit', dest='commit', type=bool, default=False,
help='Some scripts care about site id')
args, _ = parser.parse_known_args()
site_id = args.siteid
site = app[site_id] # noqa
setSite(site)
user = app.acl_users.getUser('admin') # noqa
newSecurityManager(None, user.__of__(app.acl_users)) # noqa
gsettings = GlobalSettings(site)
stats = {
'keep': 0,
'remove': 0,
'total': 0,
'obfuscated': 0
}
obfuscated_paths = {}
obfuscated_uids = []
for brain in site.portal_catalog(portal_type='File'):
stats['total'] += 1
obj = brain.getObject()
settings = Settings(obj)
if settings.obfuscated_filepath:
stats['obfuscated'] += 1
settings.obfuscate_secret
storage_dir = getResourceDirectory(gsettings=gsettings,
settings=settings)
secret_dir = os.path.join(storage_dir, settings.obfuscate_secret)
obfuscated_paths[secret_dir] = brain.UID
obfuscated_uids.append(brain.UID)
def process_directory(directory):
for sub_directory in os.listdir(directory):
if '@@' in sub_directory:
continue
sub_directory_path = os.path.join(directory, sub_directory)
if not os.path.isdir(sub_directory_path):
continue
if sub_directory_path in obfuscated_paths:
# private, obfuscated path
continue
if sub_directory in obfuscated_uids:
continue
if len(sub_directory) > 10:
# check if UID
obj = uuidToObject(sub_directory)
if obj is None:
stats['remove'] += 1
print('Could not find object related to: ' + sub_directory_path)
if args.commit:
shutil.rmtree(sub_directory_path)
continue
else:
stats['keep'] += 1
process_directory(sub_directory_path)
process_directory(gsettings.storage_location)
print('Total files %i' % stats['total'])
print('Total Obfuscated %i' % stats['obfuscated'])
print('Removed %i directories' % stats['remove'])
print('Kept %i directories' % stats['keep'])