I have a server where i have installed frappe framework and LMS app
I when i upload any video or pdf in the lessons it gets save in
sites/site1.local/public/files for public files and
sites/site1.local/private/files for private files
I only have 50 GB storage in that server and one lesson can be of 100+ MB
I cannot increase the server space as its a virtual server.
I want to change the file save location to a different Location outside my server
I have a Network Drive in which I want to store that video and PDF Files
Is there any way to do it.
Use Hooks for it.
after_insert: move the file to different location and update the path in file doctype
For uploading a document to the network drive, you first need to configure your drive in Frappe with an API key.
hooks.py:
doc_events = {
"File": {
"after_insert": "custom_app_name.controller.file_upload_to_drive",
"on_trash": "custom_app_name.controller.delete_from_drive"
}
}
controller.py
//for uploading files to drive
def file_upload_to_drive(doc, method):
"""
Check and upload files to the network drive.
"""
path = doc.file_url
# Implement authentication and connection logic for the network drive
# Get file metadata and content type
file_path = path
file_name = os.path.basename(file_path)
content_type, _ = mimetypes.guess_type(file_path)
# upload the file to the network drive
for deleting files from your network drive:
def delete_from_network_drive(doc, method=None):
"""Delete file from network drive"""
if doc.file_url:
# Modify this function to delete the file from the network drive
delete_from_drive(doc.file_url)
def delete_from_drive(file_path):
"""
Deletes a file from the network drive.
"""
# Implement the logic to delete the file from the network drive
pass
@Manav_Mandli
Thank you for your response
will this code works for my all files in all the apps of that website or i need to write different code for my custom app and frappe.
You mentioned controller.py file but when i searched the files directory i didn’t find any file name like this do i need to create a new file for that
Hi,
You don’t need to write a different code. With this code, you can upload your files from anywhere and it will automatically upload to your network drive. Yes, you should create a controller.py file in the same directory as the hook.py file in your custom_app.