File attachments- How to avoid duplicate entries error?

Hello, when I try to attach the same file using a different file name I receive a duplicate entry error when it is saved to the database. It seems to come from file_manager.py;

if not file_data:
	call_hook_method("before_write_file", file_size=file_size)

	write_file_method = get_hook_method('write_file', fallback=save_file_on_filesystem)
	file_data = write_file_method(fname, content, content_type=content_type, is_private=is_private)
	file_data = copy(file_data)

file_data.update({
	"doctype": "File",
	"attached_to_doctype": dt,
	"attached_to_name": dn,
	"folder": folder,
	"file_size": file_size,
	"content_hash": content_hash,
	"is_private": is_private
})

f = frappe.get_doc(file_data)
f.flags.ignore_permissions = True
try:
	f.insert()
except frappe.DuplicateEntryError:
	return frappe.get_doc("File", f.duplicate_entry)

I believe the file is uploaded using a random name then saved to the file system. It seems before it is saved to the database with its original name the code above attempts to insert the new record without a name. Generating the duplicate entry error. Is this correct?

  1. What is the best way to avoid this error?

I was thinking of saving each file in a unique folder on the file system.

  1. Would passing a file name with path data “/private/someuniquename/filename.txt” cause the file to be saved to to that location on the file system?

  2. Would saving the file to a different directory avoid the duplicate entry error?

Thank you for your time and patience as I try to learn this.

This is not a standard issue. Did you have some customizations?

@rmehta Yes, I was overwriting the the file name in upload.js.

I will try the suggestions you made in thread:

I will update this thread when I have tested it.

Thank you,