def get_assets(self, projection=None): """ Return a list of assets given (as JSON) in the form argument `assets`. """ uids = self.get_body_arguments('uids') if not uids: raise APIError( 'invalid_request', hint=f'No uids provided.' ) # Fetch the asset return Asset.many( And( Q.account == self.account, In(Q.uid, uids), Not(Q.expires <= time.time()) ), projection=(projection or self.DEFAULT_PROJECTION) )
def purge(): """Purge assets that have expired""" # Get expired assets now = time.time() max_delete_period = 48 * 60 * 60 assets = Asset.many(And( Q.expires <= now, Q.expires > now - max_delete_period, ), projection={ 'expires': True, 'ext': True, 'meta.length': True, 'name': True, 'secure': True, 'uid': True, 'variations': { '$sub.': Variation }, 'account': { '$ref': Account, 'public_backend_settings': True, 'secure_backend_settings': True } }) # Delete the assets for asset in assets: variation_count = 0 length = asset.meta['length'] # Remove all stored files for the asset if asset.secure: backend = asset.account.secure_backend else: backend = asset.account.public_backend if backend: if asset.variations: # Count the variations for the asset variation_count = len(asset.variations) for variation_name, variation in asset.variations.items(): # Tally up the assets total footprint length += variation.meta['length'] # Remove variation files backend.delete( variation.get_store_key(asset, variation_name)) # Remove the asset file backend.delete(asset.store_key) # Delete the asset from the database asset.delete() # Update the asset stats Stats.inc(asset.account, today_tz(), { 'assets': -1, 'variations': -variation_count, 'length': -length })