示例#1
0
文件: persist.py 项目: GetmeUK/h51
    def post(self):
        """
        Removes any existing timeout for one or more existing assets making
        the assets persistent.
        """

        assets = self.get_assets(projection={'uid': True})

        # Clear the expires timestamp (if there is one) for the assets
        Asset.get_collection().update(In(Q._id,
                                         [a._id for a in assets]).to_dict(), {
                                             '$set': {
                                                 'modified': datetime.utcnow()
                                             },
                                             '$unset': {
                                                 'expires': ''
                                             }
                                         },
                                      multi=True)

        self.write(
            {'results': [{
                'uid': a.uid,
                'expires': None
            } for a in assets]})
示例#2
0
def delete_document(state):

    Asset.get_collection().update_many(
        (Q.account == state.account._id).to_dict(),
        {
            '$set': {
                'expires': time.time() - 1,
                'modified': datetime.utcnow()
            }
        }
    )

    generic.delete['post'].super(state)
示例#3
0
    def post(self):
        """
        Removes any existing timeout for one or more existing assets making
        the assets persistent.

        Set a timeout for one or more assets. After the timeout has expired
        the assets will be automatically deleted.

        NOTE: The files associated with expired assets are delete periodically
        and therefore may still temporarily be available after the asset has
        expired and is not longer available via the API.
        """

        assets = self.get_assets(projection={'uid': True})

        # Validate the arguments
        form = ExpireForm(to_multi_dict(self.request.body_arguments))
        if not form.validate():
            raise APIError(
                'invalid_request',
                arg_errors=form.errors
            )

        form_data = form.data

        # Update the expires timestamp for the asset
        expires = time.time() + form_data['seconds']
        Asset.get_collection().update(
            In(Q._id, [a._id for a in assets]).to_dict(),
            {
                '$set': {
                    'expires': time.time() + form_data['seconds'],
                    'modified': datetime.utcnow()
                }
            },
            multi=True
        )

        self.write({
            'results': [
                {
                    'uid': a.uid,
                    'expires': expires
                }
                for a in assets
            ]
        })
示例#4
0
文件: persist.py 项目: GetmeUK/h51
    def post(self, uid):
        """
        Removes any existing timeout for an asset making the asset persistent.
        """

        asset = self.get_asset(uid, projection={'uid': True, 'expires': True})

        # Clear the expires timestamp (if there is one)
        Asset.get_collection().update({'_id': asset._id}, {
            '$set': {
                'modified': datetime.utcnow()
            },
            '$unset': {
                'expires': ''
            }
        })

        self.write({'uid': asset.uid, 'expires': None})
示例#5
0
    def _add_to_meta(self, asset, data):
        """Add the specified data to the asset's meta"""

        # Modify the asset instance
        if self.asset_type not in asset.meta:
            asset.meta[self.asset_type] = {}

        asset.meta[self.asset_type][self.name] = data
        asset.modified = datetime.utcnow()

        # Apply the updated to the database
        Asset.get_collection().update(
            (Q._id == asset._id).to_dict(),
            {
                '$set': {
                    f'meta.{self.asset_type}.{self.name}': data,
                    'modified': asset.modified
                }
            }
        )
示例#6
0
文件: document.py 项目: GetmeUK/h51
    async def delete(self, uid, variation_name):
        """Remove the variation from the asset"""

        asset = self.get_asset(uid,
                               projection={
                                   'expires': True,
                                   'name': True,
                                   'secure': True,
                                   'uid': True,
                                   f'variations.{variation_name}': {
                                       '$sub': Variation
                                   }
                               })

        variation = self.get_variation(asset, variation_name)

        # Remove file for the variation
        backend = self.get_backend(asset.secure)

        await backend.async_delete(variation.get_store_key(
            asset, variation_name),
                                   loop=asyncio.get_event_loop())

        # Remove the variation from the asset
        Asset.get_collection().update(
            (Q._id == asset._id).to_dict(),
            {'$unset': {
                f'variations.{variation_name}': ''
            }})

        # Update the asset stats
        Stats.inc(self.account, today_tz(tz=self.config['TIMEZONE']), {
            'variations': -1,
            'length': -variation.meta['length']
        })

        self.set_status(204)
        self.finish()
示例#7
0
    def _store_variation(
        self,
        config,
        asset,
        variation_name,
        versioned,
        ext,
        meta,
        file
    ):
        """
        Store a new variation of the asset. This method both stores the
        variation (and removes any existing variation with the same name) as
        well as updating the asset's `variations` field with details of the
        new variation.
        """

        # Get the account and backend associated with the asset
        account = Account.by_id(
            asset.account,
            projection={
                'public_backend_settings': True,
                'secure_backend_settings': True
            }
        )

        if asset.secure:
            backend = account.secure_backend
        else:
            backend = account.public_backend

        assert backend, 'No backend configured for the asset'

        # Ensure the asset's variation value is a dictionary (in case it's
        # never been set before).
        if not asset.variations:
            asset.variations = {}

        # Create the new variation
        old_variation = asset.variations.get(variation_name)

        new_variation = Variation(
            content_type=mimetypes.guess_type(f'f.{ext}')[0] if ext else '',
            ext=ext,
            meta=meta,
            version=(
                Variation.next_version(
                    old_variation.version if old_variation else None
                )
                if versioned else None
            )
        )

        # Store the new variation
        new_store_key = new_variation.get_store_key(asset, variation_name)
        file.seek(0)
        backend.store(file, new_store_key)

        # Add the new variation's details to the asset's `variations` field
        asset.variations[variation_name] = new_variation
        asset.modified = datetime.utcnow()

        # Apply the updated to the database
        Asset.get_collection().update(
            (Q._id == asset._id).to_dict(),
            {
                '$set': {
                    f'variations.{variation_name}': \
                            new_variation.to_json_type(),
                    'modified': asset.modified
                }
            }
        )

        # Remove the existing variation
        if old_variation:
            old_store_key = old_variation.get_store_key(asset, variation_name)
            if new_store_key != old_store_key:
                backend.delete(old_store_key)

        # Update the asset stats
        new_length = new_variation.meta['length']
        old_length = 0
        if old_variation:
            old_length = old_variation.meta['length']

        Stats.inc(
            account,
            today_tz(tz=config['TIMEZONE']),
            {
                'variations': 0 if old_variation else 1,
                'length': new_length - old_length
            }
        )