Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

13 роки тому
13 роки тому
13 роки тому
13 роки тому
12 роки тому
12 роки тому
13 роки тому
13 роки тому
13 роки тому
12 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
13 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. def upload():
  26. # get record details
  27. dt = webnotes.form_dict.doctype
  28. dn = webnotes.form_dict.docname
  29. at_id = webnotes.form_dict.at_id
  30. file_url = webnotes.form_dict.file_url
  31. filename = webnotes.form['filedata'].filename
  32. webnotes.response['type'] = 'iframe'
  33. if not filename and not file_url:
  34. webnotes.response['result'] = """
  35. <script type='text/javascript'>
  36. window.parent.wn.views.fomrview['%s'].frm.attachments.dialog.hide();
  37. window.parent.msgprint("Please upload a file or copy-paste a link (http://...)");
  38. </script>""" % dt
  39. return
  40. # save
  41. if filename:
  42. fid, fname = save_uploaded()
  43. elif file_url:
  44. fid, fname = save_url(file_url)
  45. # save it in the form
  46. updated = False
  47. if fid:
  48. updated = add_file_list(dt, dn, fname, fid)
  49. if fid and updated:
  50. # refesh the form!
  51. # with the new modified timestamp
  52. webnotes.response['result'] = """
  53. <script type='text/javascript'>
  54. window.parent.wn.ui.form.file_upload_done('%(dt)s', '%(dn)s', '%(fid)s', '%(fname)s', '%(at_id)s', '%(mod)s');
  55. window.parent.wn.views.formview['%(dt)s'].frm.show_doc('%(dn)s');
  56. </script>
  57. """ % {
  58. 'dt': dt,
  59. 'dn': dn,
  60. 'fid': fid,
  61. 'fname': fname.replace("'", "\\'"),
  62. 'at_id': at_id,
  63. 'mod': webnotes.conn.get_value(dt, dn, 'modified')
  64. }
  65. def save_uploaded():
  66. webnotes.response['type'] = 'iframe'
  67. fname, content = get_uploaded_content()
  68. if content:
  69. fid = save_file(fname, content)
  70. # fname is not valid
  71. return fid, fid
  72. else:
  73. return None, fname
  74. def save_url(file_url):
  75. if not (file_url.startswith("http://") or file_url.startswith("https://")):
  76. webnotes.msgprint("URL must start with 'http://' or 'https://'")
  77. return None, None
  78. f = webnotes.doc("File Data")
  79. f.file_url = file_url
  80. f.file_name = file_url.split('/')[-1]
  81. f.save(new=1)
  82. return f.name, file_url
  83. def get_uploaded_content():
  84. # should not be unicode when reading a file, hence using webnotes.form
  85. if 'filedata' in webnotes.form:
  86. i = webnotes.form['filedata']
  87. webnotes.uploaded_filename, webnotes.uploaded_content = i.filename, i.file.read()
  88. return webnotes.uploaded_filename, webnotes.uploaded_content
  89. else:
  90. webnotes.msgprint('No File')
  91. return None, None
  92. def save_file(fname, content, module=None):
  93. from webnotes.model.doc import Document
  94. from filecmp import cmp
  95. check_max_file_size(content)
  96. new_fname = write_file(content)
  97. # some browsers return the full path
  98. if '\\' in fname:
  99. fname = fname.split('\\')[-1]
  100. if '/' in fname:
  101. fname = fname.split('/')[-1]
  102. # we use - for versions, so remove them from the name!
  103. fname = fname.replace('-', '')
  104. fpath = os.path.join(get_files_path(), fname)
  105. if os.path.exists(fpath) and cmp(fpath, new_fname):
  106. # remove new file, already exists!
  107. os.remove(new_fname)
  108. return fname
  109. else:
  110. # generate the ID (?)
  111. f = Document('File Data')
  112. f.file_name = fname
  113. f.save(1)
  114. # rename new file
  115. os.rename(new_fname, os.path.join(get_files_path(), f.name))
  116. return f.name
  117. def check_max_file_size(content):
  118. max_file_size = getattr(conf, 'max_file_size', 1000000)
  119. if len(content) > max_file_size:
  120. raise Exception, 'Maximum File Limit (%s MB) Crossed' % (int(max_file_size / 1000000))
  121. def write_file(content):
  122. """write file to disk with a random name (to compare)"""
  123. # create account folder (if not exists)
  124. webnotes.create_folder(get_files_path())
  125. fname = os.path.join(get_files_path(), webnotes.generate_hash())
  126. # write the file
  127. with open(fname, 'w+') as f:
  128. f.write(content)
  129. return fname
  130. def add_file_list(dt, dn, fname, fid):
  131. fl = webnotes.conn.get_value(dt, dn, 'file_list') or ''
  132. if fl: fl += '\n'
  133. fl += fname + ',' + fid
  134. webnotes.conn.set_value(dt, dn, 'file_list', fl)
  135. return True
  136. def remove_all(dt, dn):
  137. """remove all files in a transaction"""
  138. file_list = webnotes.conn.get_value(dt, dn, 'file_list') or ''
  139. for afile in file_list.split('\n'):
  140. if afile:
  141. fname, fid = afile.split(',')
  142. remove_file(dt, dn, fid)
  143. def remove_file(dt, dn, fid):
  144. """Remove fid from the give file_list"""
  145. # get the old file_list
  146. fl = webnotes.conn.get_value(dt, dn, 'file_list') or ''
  147. new_fl = []
  148. fl = fl.split('\n')
  149. for f in fl:
  150. if f and f.split(',')[1]!=fid:
  151. new_fl.append(f)
  152. # delete
  153. delete_file(fid)
  154. # update the file_list
  155. webnotes.conn.set_value(dt, dn, 'file_list', '\n'.join(new_fl))
  156. # return the new timestamp
  157. return webnotes.conn.get_value(dt, dn, 'modified')
  158. def get_file_system_name(fname):
  159. # get system name from File Data table
  160. return webnotes.conn.sql("""select name, file_name from `tabFile Data`
  161. where name=%s or file_name=%s""", (fname, fname))
  162. def delete_file(fid, verbose=0):
  163. """delete file from file system"""
  164. import os
  165. webnotes.conn.sql("delete from `tabFile Data` where name=%s", fid)
  166. path = os.path.join(get_files_path(), fid.replace('/','-'))
  167. if os.path.exists(path):
  168. os.remove(path)
  169. def get_file(fname):
  170. f = get_file_system_name(fname)
  171. if f:
  172. file_id = f[0][0].replace('/','-')
  173. file_name = f[0][1]
  174. else:
  175. file_id = fname
  176. file_name = fname
  177. # read the file
  178. import os
  179. with open(os.path.join(get_files_path(), file_id), 'r') as f:
  180. content = f.read()
  181. return [file_name, content]
  182. files_path = None
  183. def get_files_path():
  184. global files_path
  185. if not files_path:
  186. import os, conf
  187. files_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)),
  188. 'public', 'files')
  189. return files_path
  190. def make_thumbnail(blob, size):
  191. from PIL import Image
  192. from cStringIO import StringIO
  193. fobj = StringIO(blob)
  194. image = Image.open(fobj)
  195. image.thumbnail((tn,tn*2), Image.ANTIALIAS)
  196. outfile = cStringIO.StringIO()
  197. image.save(outfile, 'JPEG')
  198. outfile.seek(0)
  199. fcontent = outfile.read()
  200. return fcontent