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.
 
 
 
 
 
 

207 lines
5.8 KiB

  1. # Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. #
  3. # MIT License (MIT)
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a
  6. # copy of this software and associated documentation files (the "Software"),
  7. # to deal in the Software without restriction, including without limitation
  8. # the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. # and/or sell copies of the Software, and to permit persons to whom the
  10. # Software is furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. # PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. # CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. # OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. #
  22. from __future__ import unicode_literals
  23. import webnotes
  24. import os, conf
  25. from webnotes.utils import cstr, get_path
  26. from webnotes import _
  27. class MaxFileSizeReachedError(webnotes.ValidationError): pass
  28. def upload():
  29. # get record details
  30. dt = webnotes.form_dict.doctype
  31. dn = webnotes.form_dict.docname
  32. at_id = webnotes.form_dict.at_id
  33. file_url = webnotes.form_dict.file_url
  34. filename = webnotes.form_dict.filename
  35. if not filename and not file_url:
  36. webnotes.msgprint(_("Please select a file or url"),
  37. raise_exception=True)
  38. # save
  39. if filename:
  40. fid, fname = save_uploaded(dt, dn)
  41. elif file_url:
  42. fid, fname = save_url(file_url, dt, dn)
  43. if fid:
  44. return fid
  45. def save_uploaded(dt, dn):
  46. fname, content = get_uploaded_content()
  47. if content:
  48. fid = save_file(fname, content, dt, dn)
  49. return fid, fname
  50. else:
  51. raise Exception
  52. def save_url(file_url, dt, dn):
  53. if not (file_url.startswith("http://") or file_url.startswith("https://")):
  54. webnotes.msgprint("URL must start with 'http://' or 'https://'")
  55. return None, None
  56. f = webnotes.bean({
  57. "doctype": "File Data",
  58. "file_url": file_url,
  59. "attached_to_doctype": dt,
  60. "attached_to_name": dn
  61. })
  62. f.ignore_permissions = True
  63. f.insert();
  64. return f.doc.name, file_url
  65. def get_uploaded_content():
  66. # should not be unicode when reading a file, hence using webnotes.form
  67. if 'filedata' in webnotes.form_dict:
  68. import base64
  69. webnotes.uploaded_content = base64.b64decode(webnotes.form_dict.filedata)
  70. webnotes.uploaded_filename = webnotes.form_dict.filename
  71. return webnotes.uploaded_filename, webnotes.uploaded_content
  72. else:
  73. webnotes.msgprint('No File')
  74. return None, None
  75. def save_file(fname, content, dt, dn):
  76. from filecmp import cmp
  77. files_path = get_files_path()
  78. file_size = check_max_file_size(content)
  79. temp_fname = write_file(content)
  80. fname = scrub_file_name(fname)
  81. fpath = os.path.join(files_path, fname)
  82. if os.path.exists(fpath):
  83. if cmp(fpath, temp_fname):
  84. # remove new file, already exists!
  85. os.remove(temp_fname)
  86. else:
  87. # get_new_version name
  88. fname = get_new_fname_based_on_version(files_path, fname)
  89. # rename
  90. os.rename(temp_fname, os.path.join(files_path, fname))
  91. else:
  92. # rename new file
  93. os.rename(temp_fname, os.path.join(files_path, fname))
  94. f = webnotes.bean({
  95. "doctype": "File Data",
  96. "file_name": fname,
  97. "attached_to_doctype": dt,
  98. "attached_to_name": dn,
  99. "file_size": file_size
  100. })
  101. f.ignore_permissions = True
  102. f.insert();
  103. return f.doc.name
  104. def get_new_fname_based_on_version(files_path, fname):
  105. # new version of the file is being uploaded, add a revision number?
  106. versions = filter(lambda f: f.startswith(fname), os.listdir(files_path))
  107. versions.sort()
  108. if "-" in versions[-1]:
  109. version = int(versions[-1].split("-")[-1]) or 1
  110. else:
  111. version = 1
  112. new_fname = fname + "-" + str(version)
  113. while os.path.exists(os.path.join(files_path, new_fname)):
  114. version += 1
  115. new_fname = fname + "-" + str(version)
  116. if version > 100:
  117. break # let there be an exception
  118. return new_fname
  119. def scrub_file_name(fname):
  120. if '\\' in fname:
  121. fname = fname.split('\\')[-1]
  122. if '/' in fname:
  123. fname = fname.split('/')[-1]
  124. return fname
  125. def check_max_file_size(content):
  126. max_file_size = getattr(conf, 'max_file_size', 1000000)
  127. file_size = len(content)
  128. if file_size > max_file_size:
  129. webnotes.msgprint(_("File size exceeded the maximum allowed size"),
  130. raise_exception=MaxFileSizeReachedError)
  131. return file_size
  132. def write_file(content):
  133. """write file to disk with a random name (to compare)"""
  134. # create account folder (if not exists)
  135. webnotes.create_folder(get_files_path())
  136. fname = os.path.join(get_files_path(), webnotes.generate_hash())
  137. # write the file
  138. with open(fname, 'w+') as f:
  139. f.write(content)
  140. return fname
  141. def remove_all(dt, dn):
  142. """remove all files in a transaction"""
  143. try:
  144. for fid in webnotes.conn.sql_list("""select name from `tabFile Data` where
  145. attached_to_doctype=%s and attached_to_name=%s""", (dt, dn)):
  146. remove_file(fid)
  147. except Exception, e:
  148. if e.args[0]!=1054: raise e # (temp till for patched)
  149. def remove_file(fid):
  150. """Remove file and File Data entry"""
  151. webnotes.delete_doc("File Data", fid)
  152. def get_file_system_name(fname):
  153. # get system name from File Data table
  154. return webnotes.conn.sql("""select name, file_name from `tabFile Data`
  155. where name=%s or file_name=%s""", (fname, fname))
  156. def get_file(fname):
  157. f = get_file_system_name(fname)
  158. if f:
  159. file_name = f[0][1]
  160. else:
  161. file_name = fname
  162. # read the file
  163. import os
  164. with open(os.path.join(get_files_path(), file_name), 'r') as f:
  165. content = f.read()
  166. return [file_name, content]
  167. files_path = None
  168. def get_files_path():
  169. global files_path
  170. if not files_path:
  171. files_path = get_path("public", "files")
  172. return files_path