예제 #1
0
def _get_preview(ufile):
  # deal with the preview
  if ufile.mimetype in ['image/jpeg', 'image/png', 'image/bmp', 'image/gif']:
    preview = 'image'
  elif ufile.mimetype in ['text/plain']:
    preview = 'text'
    enc_detector = UniversalDetector()
    file_lines = []
    try:
      with open(funcs.fullname(ufile.filename), 'r') as f:
        for line in f:
          file_lines.append(line)
          if not enc_detector.done:
            enc_detector.feed(line)
      enc_detector.close()
      def unicodefy(s):
        return unicode(s, enc_detector.result.get('encoding'), errors='ignore')
      file_lines = map(unicodefy, file_lines)
      ufile.file_content = '<p>%s</p>' % '</p><p>'.join(file_lines)
    except:
      ufile.file_content = 'failed to open file'
      
    
    
  else:
    preview = 'icon'
  return preview
예제 #2
0
def delete_file(file_indicator=None):
  if not funcs.is_admin_login(): abort(403)
  ufile = UFile.query.filter(UFile.url == file_indicator).first()
  if not ufile: abort(404)
  
  fn = funcs.fullname(ufile.filename)
  db_session.delete(ufile)
  db_session.commit()
  try:
    os.remove(fn)
  except:
    pass
  return redirect(url_for('file_serve',file_indicator=file_indicator))