Пример #1
0
    def delete(self, id, user):
        stem_image = self.load(id, user=user, level=AccessType.WRITE)

        if not stem_image:
            raise RestException('StemImage not found.', 404)

        public = stem_image.get('public', False)

        # Try to load the file and check if it was imported.
        # If it was imported, delete the item containing the file.
        # If loading/removing the file fails, remove the stem image anyways
        try:
            f = FileModel().load(stem_image['fileId'],
                                 level=AccessType.WRITE,
                                 user=user)
            if f.get('imported', False) is True:
                item = ItemModel().load(f['itemId'],
                                        level=AccessType.WRITE,
                                        user=user)
                if item['folderId'] == self._get_import_folder(user,
                                                               public)['_id']:
                    ItemModel().remove(item)
        except:
            pass

        return self.remove(stem_image)
Пример #2
0
def _getFolderReadme(folder):
    query = {
        'folderId': folder['_id'],
        'name': {
            '$regex': re.compile(r'^README(\..+)?$')
        },
    }
    item = ItemModel().findOne(query)
    if item:
        files = list(ItemModel().childFiles(item=item, limit=1))
        if len(files) >= 1:
            return FileModel().download(files[0])
    cherrypy.response.status = 204
    return ''
Пример #3
0
    def _create_import_item(self, user, name, public=False):
        """Create a new item and put it in the import folder.

        The new item will be returned.
        """
        folder = self._get_import_folder(user, public)

        return ItemModel().createItem(name, user, folder)
Пример #4
0
    def __init__(self):
        super(Item, self).__init__()
        self.resourceName = 'item'
        self._model = ItemModel()

        self.route('DELETE', (':id',), self.deleteItem)
        self.route('GET', (), self.find)
        self.route('GET', (':id',), self.getItem)
        self.route('GET', (':id', 'files'), self.getFiles)
        self.route('GET', (':id', 'download'), self.download)
        self.route('GET', (':id', 'rootpath'), self.rootpath)
        self.route('POST', (), self.createItem)
        self.route('PUT', (':id',), self.updateItem)
        self.route('POST', (':id', 'copy'), self.copyItem)
        self.route('PUT', (':id', 'metadata'), self.setMetadata)
        self.route('DELETE', (':id', 'metadata'), self.deleteMetadata)
Пример #5
0
def computeCollectionConvexHull(collection, currentUser):
    docs = ItemModel().findWithPermissions(query={
        'baseParentId': collection['_id'],
        'geometa': {
            '$exists': True
        }
    },
                                           user=currentUser)

    convexHull = None
    collectionItems = [doc['geometa']['bounds'] for doc in docs]

    if collectionItems:
        polygons = [Polygon(*item['coordinates']) for item in collectionItems]
        convexHull = MultiPolygon(polygons).convex_hull

    return convexHull
Пример #6
0
def itemRemovedFromCollection(self, event):
    # More efficient way instead of whole recomputation?
    item = ItemModel().load(event.info['_id'], user=self.getCurrentUser())

    if item['baseParentType'] != 'collection':
        return
    collection = CollectionModel().load(item['baseParentId'],
                                        user=self.getCurrentUser())

    # Default to just bounding box of item itself
    newConvexHull = computeCollectionConvexHull(collection,
                                                self.getCurrentUser())

    collection[BOUNDS_KEY] = {
        'type': 'Polygon',
        'coordinates': [list(newConvexHull.exterior.coords)]
    }
    collection = CollectionModel().save(collection)
Пример #7
0
    def __init__(self):
        super().__init__()
        self.resourceName = 'item'
        self._model = ItemModel()

        self.route('DELETE', (':id',), self.deleteItem)
        self.route('GET', (), self.find)
        self.route('GET', (':id',), self.getItem)
        self.route('GET', (':id', 'files'), self.getFiles)
        self.route('GET', (':id', 'download'), self.download)
        self.route('GET', (':id', 'rootpath'), self.rootpath)
        self.route('GET', (':id', 'position'), self.findPosition)
        self.route('POST', (), self.createItem)
        self.route('PUT', (':id',), self.updateItem)
        self.route('POST', (':id', 'copy'), self.copyItem)
        self.route('PUT', (':id', 'metadata'), self.setMetadata)
        self.route('DELETE', (':id', 'metadata'), self.deleteMetadata)
        #added by Shubhang on 23rd nov 
        self.route('PUT', ('moveItems',), self.moveItems)
Пример #8
0
def itemAddedToCollection(self, event):
    item = ItemModel().load(event.info['file']['itemId'],
                            user=self.getCurrentUser())

    if item['baseParentType'] != 'collection' or 'geometa' not in item:
        return

    collection = CollectionModel().load(item['baseParentId'],
                                        user=self.getCurrentUser())

    # Default to just bounding box of item itself
    newConvexHull = Polygon(*item['geometa']['bounds']['coordinates'])

    if BOUNDS_KEY in collection:
        oldConvexHull = Polygon(*collection[BOUNDS_KEY]['coordinates'])
        newConvexHull = MultiPolygon(
            polygons=[oldConvexHull, newConvexHull]).convex_hull

    collection[BOUNDS_KEY] = {
        'type': 'Polygon',
        'coordinates': [list(newConvexHull.exterior.coords)]
    }
    collection = CollectionModel().save(collection)
Пример #9
0
def load(info):
    KB = 1024
    MB = 1024 * KB
    GB = 1024 * MB
    SettingDefault.defaults[PluginSettings.PRIVATE_STORAGE_PATH] = '/tmp/ps'
    SettingDefault.defaults[PluginSettings.PRIVATE_STORAGE_CAPACITY] = 100 * GB
    # run collection every 10 minutes
    SettingDefault.defaults[PluginSettings.GC_RUN_INTERVAL] = 10 * 60
    # only collect if over %50 used
    SettingDefault.defaults[PluginSettings.GC_COLLECT_START_FRACTION] = 0.5
    # stop collecting when below %50 usage
    SettingDefault.defaults[PluginSettings.GC_COLLECT_END_FRACTION] = 0.5

    settings = Setting()
    session = Session()
    lock = Lock()
    transfer = Transfer()
    fs = FS()

    pathMapper = path_mapper.PathMapper(settings)
    transferManager = transfer_manager.SimpleTransferManager(
        settings, pathMapper)

    # a GC that does nothing
    # fileGC = file_gc.DummyFileGC(settings, pathMapper)

    fileGC = file_gc.PeriodicFileGC(
        settings,
        pathMapper,  # noqa E128
        file_gc.CollectionStrategy(
            file_gc.FractionalCollectionThresholds(settings),
            file_gc.LRUSortingScheme()))
    cacheManager = cache_manager.SimpleCacheManager(settings, transferManager,
                                                    fileGC, pathMapper)
    dm = DM(cacheManager)
    info['apiRoot'].dm = dm
    info['apiRoot'].dm.route('GET', ('session', ), session.listSessions)
    info['apiRoot'].dm.route('GET', (
        'session',
        ':id',
    ), session.getSession)
    info['apiRoot'].dm.route('POST', ('session', ), session.createSession)
    info['apiRoot'].dm.route('PUT', (
        'session',
        ':id',
    ), session.modifySession)
    info['apiRoot'].dm.route('DELETE', ('session', ':id'),
                             session.removeSession)

    info['apiRoot'].dm.route('POST', ('lock', ), lock.acquireLock)
    info['apiRoot'].dm.route('DELETE', ('lock', ':id'), lock.releaseLock)
    info['apiRoot'].dm.route('GET', ('lock', ':id'), lock.getLock)
    info['apiRoot'].dm.route('GET', ('lock', ), lock.listLocks)
    info['apiRoot'].dm.route('GET', ('lock', ':id', 'download'),
                             lock.downloadItem)

    info['apiRoot'].dm.route('GET', ('session', ':id', 'object'),
                             session.getObject)
    info['apiRoot'].dm.route('GET', (
        'session',
        ':id',
        'lock',
    ), lock.listLocksForSession)
    info['apiRoot'].dm.route('GET', ('session', ':id', 'transfer'),
                             transfer.listTransfersForSession)

    info['apiRoot'].dm.route('GET', ('transfer', ), transfer.listTransfers)

    info['apiRoot'].dm.route('GET', ('fs', 'item', ':itemId'),
                             fs.getItemUnfiltered)
    info['apiRoot'].dm.route('GET', ('fs', ':id', 'raw'), fs.getRawObject)
    info['apiRoot'].dm.route('PUT', ('fs', ':id', 'setProperties'),
                             fs.setProperties)
    info['apiRoot'].dm.route('GET', ('fs', ':id', 'listing'), fs.getListing)
    info['apiRoot'].dm.route('GET', ('fs', ':id', 'evict'), lock.evict)

    info['apiRoot'].dm.route('PUT', ('clearCache', ), dm.clearCache)

    def itemLocked(event):
        dict = event.info
        cacheManager.itemLocked(dict['user'], dict['itemId'],
                                dict['sessionId'])

    def itemUnlocked(event):
        cacheManager.itemUnlocked(event.info)

    def fileDownloaded(event):
        cacheManager.fileDownloaded(event.info)

    def sessionCreated(event):
        cacheManager.sessionCreated(event.info)

    def sessionDeleted(event):
        cacheManager.sessionDeleted(event.info)

    events.bind('dm.sessionCreated', 'sessionCreated', sessionCreated)
    events.bind('dm.sessionDeleted', 'sessionDeleted', sessionDeleted)
    # TODO: add session file changes
    events.bind('dm.itemLocked', 'itemLocked', itemLocked)
    events.bind('dm.itemUnlocked', 'itemUnlocked', itemUnlocked)
    events.bind('dm.fileDownloaded', 'fileDownloaded', fileDownloaded)
    ItemModel().exposeFields(level=AccessType.READ, fields={'dm'})