You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

199 line
5.5 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import webnotes
  5. import os, conf
  6. from webnotes.utils import cstr, get_path, cint
  7. from webnotes import _
  8. class MaxFileSizeReachedError(webnotes.ValidationError): pass
  9. def upload():
  10. # get record details
  11. dt = webnotes.form_dict.doctype
  12. dn = webnotes.form_dict.docname
  13. file_url = webnotes.form_dict.file_url
  14. filename = webnotes.form_dict.filename
  15. if not filename and not file_url:
  16. webnotes.msgprint(_("Please select a file or url"),
  17. raise_exception=True)
  18. # save
  19. if filename:
  20. filedata = save_uploaded(dt, dn)
  21. elif file_url:
  22. filedata = save_url(file_url, dt, dn)
  23. return {"fid": filedata.name, "filename": filedata.file_name or filedata.file_url }
  24. def save_uploaded(dt, dn):
  25. fname, content = get_uploaded_content()
  26. if content:
  27. return save_file(fname, content, dt, dn)
  28. else:
  29. raise Exception
  30. def save_url(file_url, dt, dn):
  31. if not (file_url.startswith("http://") or file_url.startswith("https://")):
  32. webnotes.msgprint("URL must start with 'http://' or 'https://'")
  33. return None, None
  34. f = webnotes.bean({
  35. "doctype": "File Data",
  36. "file_url": file_url,
  37. "attached_to_doctype": dt,
  38. "attached_to_name": dn
  39. })
  40. f.ignore_permissions = True
  41. f.insert();
  42. return f.doc
  43. def get_uploaded_content():
  44. # should not be unicode when reading a file, hence using webnotes.form
  45. if 'filedata' in webnotes.form_dict:
  46. import base64
  47. webnotes.uploaded_content = base64.b64decode(webnotes.form_dict.filedata)
  48. webnotes.uploaded_filename = webnotes.form_dict.filename
  49. return webnotes.uploaded_filename, webnotes.uploaded_content
  50. else:
  51. webnotes.msgprint('No File')
  52. return None, None
  53. def save_file(fname, content, dt, dn):
  54. import filecmp
  55. from webnotes.model.code import load_doctype_module
  56. files_path = get_path("public", "files")
  57. module = load_doctype_module(dt, webnotes.conn.get_value("DocType", dt, "module"))
  58. if hasattr(module, "attachments_folder"):
  59. files_path = os.path.join(files_path, module.attachments_folder)
  60. file_size = check_max_file_size(content)
  61. temp_fname = write_file(content, files_path)
  62. fname = scrub_file_name(fname)
  63. fpath = os.path.join(files_path, fname)
  64. fname_parts = fname.split(".", -1)
  65. main = ".".join(fname_parts[:-1])
  66. extn = fname_parts[-1]
  67. versions = get_file_versions(files_path, main, extn)
  68. if versions:
  69. found_match = False
  70. for version in versions:
  71. if filecmp.cmp(os.path.join(files_path, version), temp_fname):
  72. # remove new file, already exists!
  73. os.remove(temp_fname)
  74. fname = version
  75. found_match = True
  76. break
  77. if not found_match:
  78. # get_new_version name
  79. fname = get_new_fname_based_on_version(files_path, main, extn, versions)
  80. # rename
  81. os.rename(temp_fname, os.path.join(files_path, fname))
  82. else:
  83. # rename new file
  84. os.rename(temp_fname, os.path.join(files_path, fname))
  85. f = webnotes.bean({
  86. "doctype": "File Data",
  87. "file_name": os.path.relpath(os.path.join(files_path, fname), get_path("public")),
  88. "attached_to_doctype": dt,
  89. "attached_to_name": dn,
  90. "file_size": file_size
  91. })
  92. f.ignore_permissions = True
  93. f.insert();
  94. return f.doc
  95. def get_file_versions(files_path, main, extn):
  96. return filter(lambda f: f.startswith(main) and f.endswith(extn), os.listdir(files_path))
  97. def get_new_fname_based_on_version(files_path, main, extn, versions):
  98. versions.sort()
  99. if "-" in versions[-1]:
  100. version = cint(versions[-1].split("-")[-1]) or 1
  101. else:
  102. version = 1
  103. new_fname = main + "-" + str(version) + "." + extn
  104. while os.path.exists(os.path.join(files_path, new_fname)):
  105. version += 1
  106. new_fname = main + "-" + str(version) + "." + extn
  107. if version > 100:
  108. webnotes.msgprint("Too many versions", raise_exception=True)
  109. return new_fname
  110. def scrub_file_name(fname):
  111. if '\\' in fname:
  112. fname = fname.split('\\')[-1]
  113. if '/' in fname:
  114. fname = fname.split('/')[-1]
  115. return fname
  116. def check_max_file_size(content):
  117. max_file_size = getattr(conf, 'max_file_size', 1000000)
  118. file_size = len(content)
  119. if file_size > max_file_size:
  120. webnotes.msgprint(_("File size exceeded the maximum allowed size"),
  121. raise_exception=MaxFileSizeReachedError)
  122. return file_size
  123. def write_file(content, files_path):
  124. """write file to disk with a random name (to compare)"""
  125. # create account folder (if not exists)
  126. webnotes.create_folder(files_path)
  127. fname = os.path.join(files_path, webnotes.generate_hash())
  128. # write the file
  129. with open(fname, 'w+') as f:
  130. f.write(content)
  131. return fname
  132. def remove_all(dt, dn):
  133. """remove all files in a transaction"""
  134. try:
  135. for fid in webnotes.conn.sql_list("""select name from `tabFile Data` where
  136. attached_to_doctype=%s and attached_to_name=%s""", (dt, dn)):
  137. remove_file(fid)
  138. except Exception, e:
  139. if e.args[0]!=1054: raise e # (temp till for patched)
  140. def remove_file(fid):
  141. """Remove file and File Data entry"""
  142. webnotes.delete_doc("File Data", fid)
  143. def get_file(fname):
  144. f = webnotes.conn.sql("""select file_name from `tabFile Data`
  145. where name=%s or file_name=%s""", (fname, fname))
  146. if f:
  147. file_name = f[0][0]
  148. else:
  149. file_name = fname
  150. # read the file
  151. import os
  152. files_path = get_path("public", "files")
  153. file_path = os.path.join(files_path, file_name)
  154. if not os.path.exists(file_path):
  155. # check in folders
  156. for basepath, folders, files in os.walk(files_path):
  157. if file_name in files:
  158. file_path = os.path.join(basepath, file_name)
  159. break
  160. with open(file_path, 'r') as f:
  161. content = f.read()
  162. return [file_name, content]