pyeve / eve

REST API framework designed for human beings
https://python-eve.org
Other
6.7k stars 747 forks source link

deleteitem_internal on media resource, KeyError: 'media' #1484

Open zaterio opened 2 years ago

zaterio commented 2 years ago

Environment

EXTENDED_MEDIA_INFO = ['content_type', 'name', 'length'] RETURN_MEDIA_AS_BASE64_STRING = False RETURN_MEDIA_AS_URL = True MEDIA_ENDPOINT = 'media'

I'm trying to delete files on the media endpoint, with deleteitem_internal, but apparently deleteitem_internal does not consider the media endpoint when is enabled, the same could be happening for other internal methods. It will also be valuable if when deleting an item from media (with deleteitem_internal), related items in the fs.files and fs.chunks collections are also deleted

Example:

fs.files:

_id: 62fa8c38f4c4954b8fef7e71 filename: "pic.jpg" contentType: "image/jpg" chunkSize: 261120 length: 48640 uploadDate: 2022-08-15T18:11:04.278+00:00

fs.chunks: _id: 62fa8c38f4c4954b8fef7e72 files_id :62fa8c38f4c4954b8fef7e71 n: 0 data: BinData(0, '/9j/4AAQSkZJRgABAgAAAQABAAD//gAQTGF2YzU4LjkxLjEwMAD/2wBDAAgKCgsKCw0NDQ0NDRAPEBAQEBAQEBAQEBASEhIVFRUS…')

media_lookup = {'_id': 62fa8c38f4c4954b8fef7e71 } deleteitem_internal('media', concurrency_check=False, suppress_callbacks=True, original=None, **media_lookup)

File "/home/trz/pushpull/lib/python3.9/site-packages/eve/methods/delete.py", line 99, in deleteitem_internal resource_def = config.DOMAIN[resource] KeyError: 'media'

same error is triggered when trying : deleteitem_internal('fs.files', concurrency_check=False, suppress_callbacks=True, original=None, **media_lookup)

Regards

ljluestc commented 4 weeks ago
from eve.methods.delete import deleteitem_internal
from pymongo import MongoClient

# Define media lookup
media_lookup = {'_id': '62fa8c38f4c4954b8fef7e71'}

# Delete the media item
deleteitem_internal('media', concurrency_check=False, suppress_callbacks=True, original=None, **media_lookup)

# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['your_database_name']

# Define file ID
file_id = '62fa8c38f4c4954b8fef7e71'

# Delete related files and chunks
db.fs.files.delete_one({'_id': file_id})
db.fs.chunks.delete_many({'files_id': file_id})