Ejemplo n.º 1
0
def migrate_old_storage(context):
    catalog = getToolByName(context, 'portal_catalog')
    portal = getSite()
    gsettings = GlobalSettings(portal)
    for brain in catalog(object_provides=IFileContent.__identifier__):
        file = brain.getObject()
        if file.getLayout() == 'documentviewer':
            settings = Settings(file)
            if settings.storage_version == 1:
                if settings.storage_type == 'File':
                    current_location = storage.getResourceDirectory(
                        gsettings=gsettings, settings=settings)
                    if not exists(current_location):
                        raise Exception(
                            "oops, can't find storage location %s" %
                            (current_location))
                    settings.storage_version = STORAGE_VERSION
                    new_location = storage.getResourceDirectory(
                        gsettings=gsettings, settings=settings)
                    # only make base
                    mkdir_p(
                        os.path.sep.join(new_location.split(os.path.sep)[:-1]))
                    shutil.move(current_location, new_location)
                    # need to commit these eagerly since a failed
                    # migration could leave some migrated wrong
                    transaction.commit()
                else:
                    settings.storage_version = STORAGE_VERSION
Ejemplo n.º 2
0
def migrate_old_storage(context):
    catalog = getToolByName(context, 'portal_catalog')
    portal = getSite()
    gsettings = GlobalSettings(portal)
    for brain in catalog(object_provides=IFileContent.__identifier__):
        file = brain.getObject()
        if file.getLayout() == 'documentviewer':
            settings = Settings(file)
            if settings.storage_version == 1:
                if settings.storage_type == 'File':
                    current_location = storage.getResourceDirectory(
                        gsettings=gsettings, settings=settings)
                    if not exists(current_location):
                        raise Exception(
                            "oops, can't find storage location %s" % (
                                current_location))
                    settings.storage_version = STORAGE_VERSION
                    new_location = storage.getResourceDirectory(
                        gsettings=gsettings, settings=settings)
                    # only make base
                    mkdir_p(os.path.sep.join(
                        new_location.split(os.path.sep)[:-1]))
                    shutil.move(current_location, new_location)
                    # need to commit these eagerly since a failed
                    # migration could leave some migrated wrong
                    transaction.commit()
                else:
                    settings.storage_version = STORAGE_VERSION
def handle_workflow_change(obj, event):
    settings = Settings(obj)
    site = getPortal(obj)
    gsettings = GlobalSettings(site)
    if not gsettings.storage_obfuscate or \
            settings.storage_type != 'File':
        return

    for perm in obj.rolesOfPermission("View"):
        if perm['name'] == 'Anonymous' and perm["selected"] != "":
            # anon can now view, move it to normal
            storage_dir = storage_dir = storage.getResourceDirectory(
                gsettings=gsettings, settings=settings)
            secret_dir = os.path.join(storage_dir,
                                      settings.obfuscate_secret)
            if not os.path.exists(secret_dir):
                # already public, oops
                return

            for folder in os.listdir(secret_dir):
                path = os.path.join(secret_dir, folder)
                newpath = os.path.join(storage_dir, folder)
                shutil.move(path, newpath)

            shutil.rmtree(secret_dir)
            settings.obfuscated_filepath = False
            return

    # if we made it here, the item might have been switched back
    # to being unpublished. Let's just get the converter object
    # and re-move it
    converter = Converter(object)
    converter.handleFileObfuscation()
Ejemplo n.º 4
0
def handle_workflow_change(object, event):
    settings = Settings(object)
    site = getPortal(object)
    gsettings = GlobalSettings(site)
    if not gsettings.storage_obfuscate or \
            settings.storage_type != 'File':
        return
    for perm in object.rolesOfPermission("View"):
        if perm['name'] == 'Anonymous' and perm["selected"] != "":
            # anon can now view, move it to normal
            storage_dir = storage_dir = storage.getResourceDirectory(
                gsettings=gsettings, settings=settings)
            secret_dir = os.path.join(storage_dir, settings.obfuscate_secret)
            if not os.path.exists(secret_dir):
                # already public, oops
                return
            for folder in os.listdir(secret_dir):
                path = os.path.join(secret_dir, folder)
                newpath = os.path.join(storage_dir, folder)
                shutil.move(path, newpath)
            shutil.rmtree(secret_dir)
            settings.obfuscated_filepath = False
            return
    # if we made it here, the item might have been switched back
    # to being unpublished. Let's just get the converter object
    # and re-move it
    converter = Converter(object)
    converter.handleFileObfuscation()
def uninstall(context, reinstall=False):
    if not reinstall:
        portal = getSite()
        portal_actions = getToolByName(portal, 'portal_actions')
        object_buttons = portal_actions.object

        # remove actions
        actions_to_remove = ('documentviewer_settings',
                             'documentviewer_convert')
        for action in actions_to_remove:
            if action in object_buttons.objectIds():
                object_buttons.manage_delObjects([action])

        catalog = getToolByName(portal, 'portal_catalog')
        objs = catalog(object_provides=OBJECT_PROVIDES)
        settings = GlobalSettings(portal)

        # remove annotations and reset view
        for obj in objs:
            obj = obj.getObject()
            if obj.getLayout() == 'documentviewer':
                obj.layout = ''
            annotations = IAnnotations(obj)
            data = annotations.get('collective.documentviewer', None)
            if data:
                del annotations['collective.documentviewer']
            # delete files associated with it...
            storage_dir = storage.getResourceDirectory(gsettings=settings,
                                                       obj=obj)
            if os.path.exists(storage_dir):
                shutil.rmtree(storage_dir)

        # remove view
        types = getToolByName(portal, 'portal_types')
        filetype = types['File']
        methods = list(filetype.view_methods)
        if 'documentviewer' in methods:
            methods.remove('documentviewer')
            filetype.view_methods = tuple(methods)

        # remove pdf album view
        for portal_type in GROUP_VIEW_DISPLAY_TYPES:
            if portal_type in types.objectIds():
                _type = types[portal_type]
                methods = list(_type.view_methods)
                if 'dvpdf-group-view' in methods:
                    methods.remove('dvpdf-group-view')
                    _type.view_methods = tuple(set(methods))

        # remove control panel
        pcp = getToolByName(context, 'portal_controlpanel')
        pcp.unregisterConfiglet('documentviewer')
        pcp.unregisterConfiglet('documentviewer-jobs')

        # remove global settings annotations
        annotations = IAnnotations(portal)
        data = annotations.get('collective.documentviewer', None)
        if data:
            del annotations['collective.documentviewer']
Ejemplo n.º 6
0
def uninstall(context, reinstall=False):
    if not reinstall:
        portal = getSite()
        portal_actions = getToolByName(portal, 'portal_actions')
        object_buttons = portal_actions.object

        # remove actions
        actions_to_remove = ('documentviewer_settings',
                             'documentviewer_convert')
        for action in actions_to_remove:
            if action in object_buttons.objectIds():
                object_buttons.manage_delObjects([action])

        catalog = getToolByName(portal, 'portal_catalog')
        objs = catalog(object_provides=IFileContent.__identifier__)
        settings = GlobalSettings(portal)

        # remove annotations and reset view
        for obj in objs:
            obj = obj.getObject()
            if obj.getLayout() == 'documentviewer':
                obj.layout = ''
            annotations = IAnnotations(obj)
            data = annotations.get('collective.documentviewer', None)
            if data:
                del annotations['collective.documentviewer']
            # delete files associated with it...
            storage_dir = storage.getResourceDirectory(gsettings=settings,
                obj=obj)
            if os.path.exists(storage_dir):
                shutil.rmtree(storage_dir)

        # remove view
        types = getToolByName(portal, 'portal_types')
        filetype = types['File']
        methods = list(filetype.view_methods)
        if 'documentviewer' in methods:
            methods.remove('documentviewer')
            filetype.view_methods = tuple(methods)

        # remove pdf album view
        for portal_type in GROUP_VIEW_DISPLAY_TYPES:
            if portal_type in types.objectIds():
                _type = types[portal_type]
                methods = list(_type.view_methods)
                if 'dvpdf-group-view' in methods:
                    methods.remove('dvpdf-group-view')
                    _type.view_methods = tuple(set(methods))

        # remove control panel
        pcp = getToolByName(context, 'portal_controlpanel')
        pcp.unregisterConfiglet('documentviewer')
        pcp.unregisterConfiglet('documentviewer-jobs')

        # remove global settings annotations
        annotations = IAnnotations(portal)
        data = annotations.get('collective.documentviewer', None)
        if data:
            del annotations['collective.documentviewer']
 def test_retrieve_correct_resource_location_old_storage(self):
     gsettings = GlobalSettings(self.portal)
     _dir = mkdtemp()
     gsettings.storage_location = _dir
     fi = self.createFile('test.pdf')
     settings = Settings(fi)
     del settings._metadata['storage_version']
     self.assertEquals(storage.getResourceDirectory(obj=fi),
                       join(_dir, fi.UID()))
Ejemplo n.º 8
0
 def test_retrieve_correct_resource_location_old_storage(self):
     gsettings = GlobalSettings(self.portal)
     _dir = mkdtemp()
     gsettings.storage_location = _dir
     fi = self.createFile('test.pdf')
     settings = Settings(fi)
     del settings._metadata['storage_version']
     self.assertEquals(storage.getResourceDirectory(obj=fi),
                       join(_dir, fi.UID()))
Ejemplo n.º 9
0
 def test_retrieve_correct_resource_location_new_storage(self):
     gsettings = GlobalSettings(self.portal)
     _dir = mkdtemp()
     gsettings.storage_location = _dir
     fi = self.createFile('test.pdf')
     notify(ObjectInitializedEvent(fi))
     uid = fi.UID()
     self.assertEquals(storage.getResourceDirectory(obj=fi),
                       join(_dir, uid[0], uid[1], uid))
Ejemplo n.º 10
0
 def get_storage_dir(self):
     if self.gsettings.storage_type == 'Blob':
         storage_dir = tempfile.mkdtemp()
     else:
         storage_dir = storage.getResourceDirectory(
             gsettings=self.gsettings, settings=self.settings)
         if not os.path.exists(storage_dir):
             mkdir_p(storage_dir)
     return storage_dir
 def test_retrieve_correct_resource_location_new_storage(self):
     gsettings = GlobalSettings(self.portal)
     _dir = mkdtemp()
     gsettings.storage_location = _dir
     fi = self.createFile('test.pdf')
     notify(ObjectInitializedEvent(fi))
     uid = fi.UID()
     self.assertEquals(storage.getResourceDirectory(obj=fi),
                       join(_dir, uid[0], uid[1], uid))
def handle_file_delete(obj, event):
    if obj.portal_type == "Image":
        return

    # need to remove files if stored in file system
    settings = Settings(obj)
    if settings.storage_type == "File":
        storage_directory = getResourceDirectory(obj=obj)
        if os.path.exists(storage_directory):
            shutil.rmtree(storage_directory)
Ejemplo n.º 13
0
def handle_file_delete(obj, event):
    if obj.portal_type == 'Image':
        return

    # need to remove files if stored in file system
    settings = Settings(obj)
    if settings.storage_type == 'File':
        storage_directory = getResourceDirectory(obj=obj)
        if os.path.exists(storage_directory):
            shutil.rmtree(storage_directory)
Ejemplo n.º 14
0
    def get_storage_dir(self):
        if self.gsettings.storage_type == 'Blob':
            storage_dir = tempfile.mkdtemp()
        else:
            storage_dir = storage.getResourceDirectory(gsettings=self.gsettings,
                                                       settings=self.settings)
            if not os.path.exists(storage_dir):
                mkdir_p(storage_dir)

        return storage_dir
 def test_migrate_old_storage(self):
     gsettings = GlobalSettings(self.portal)
     _dir = mkdtemp()
     gsettings.storage_location = _dir
     gsettings.storage_type = 'File'
     fi = self.createFile('test.pdf')
     settings = Settings(fi)
     del settings._metadata['storage_version']
     fi.reindexObject()
     notify(ObjectInitializedEvent(fi))
     self.assertEquals(settings.storage_version, 1)
     old_path = storage.getResourceDirectory(obj=fi)
     self.assertTrue(exists(old_path))
     from collective.documentviewer.upgrades import migrate_old_storage
     migrate_old_storage(self.portal)
     self.assertTrue(not exists(old_path))
     self.assertEquals(settings.storage_version, STORAGE_VERSION)
     new_path = storage.getResourceDirectory(obj=fi)
     self.assertTrue(exists(new_path))
     self.assertEquals(len(listdir(new_path)), 4)
 def test_migrate_old_storage(self):
     gsettings = GlobalSettings(self.portal)
     _dir = mkdtemp()
     gsettings.storage_location = _dir
     gsettings.storage_type = 'File'
     fi = self.createFile('test.pdf')
     settings = Settings(fi)
     del settings._metadata['storage_version']
     fi.reindexObject()
     notify(ObjectInitializedEvent(fi))
     self.assertEquals(settings.storage_version, 1)
     old_path = storage.getResourceDirectory(obj=fi)
     self.assertTrue(exists(old_path))
     from collective.documentviewer.upgrades import migrate_old_storage
     migrate_old_storage(self.portal)
     self.assertTrue(not exists(old_path))
     self.assertEquals(settings.storage_version, STORAGE_VERSION)
     new_path = storage.getResourceDirectory(obj=fi)
     self.assertTrue(exists(new_path))
     self.assertEquals(len(listdir(new_path)), 6)
Ejemplo n.º 17
0
def handle_file_delete(obj, event):
    if obj.portal_type == 'Image':
        return
    if getRequest().get('plone.app.contenttypes_migration_running', False):
        """Don't migrate while running a plone.app.contenttypes migration.
        """
        return

    # need to remove files if stored in file system
    settings = Settings(obj)
    if settings.storage_type == 'File':
        storage_directory = getResourceDirectory(obj=obj)
        if os.path.exists(storage_directory):
            shutil.rmtree(storage_directory)
Ejemplo n.º 18
0
    def test_saves_with_file_storage(self):
        fi = self.createFile('test.odp')
        gsettings = GlobalSettings(self.portal)
        gsettings.auto_select_layout = True
        gsettings.auto_layout_file_types = ['ppt']
        gsettings.storage_type = 'File'
        _dir = mkdtemp()
        gsettings.storage_location = _dir
        notify(ObjectInitializedEvent(fi))

        fi_dir = storage.getResourceDirectory(obj=fi)
        self.assertEqual(len(listdir(fi_dir)), 4)
        self.assertEqual(len(listdir(join(fi_dir, 'normal'))), 1)
        self.assertEqual(len(listdir(join(fi_dir, 'small'))), 1)
        self.assertEqual(len(listdir(join(fi_dir, 'large'))), 1)
        self.assertEqual(len(listdir(join(fi_dir, 'text'))), 1)
        shutil.rmtree(fi_dir)
Ejemplo n.º 19
0
    def test_saves_with_file_storage(self):
        fi = self.createFile('test.odp')
        gsettings = GlobalSettings(self.portal)
        gsettings.auto_select_layout = True
        gsettings.auto_layout_file_types = ['ppt']
        gsettings.storage_type = 'File'
        _dir = mkdtemp()
        gsettings.storage_location = _dir
        notify(ObjectInitializedEvent(fi))

        fi_dir = storage.getResourceDirectory(obj=fi)
        self.assertEqual(len(listdir(fi_dir)), 4)
        self.assertEqual(len(listdir(join(fi_dir, 'normal'))), 1)
        self.assertEqual(len(listdir(join(fi_dir, 'small'))), 1)
        self.assertEqual(len(listdir(join(fi_dir, 'large'))), 1)
        self.assertEqual(len(listdir(join(fi_dir, 'text'))), 1)
        shutil.rmtree(fi_dir)
Ejemplo n.º 20
0
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
    '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