def view_shared_file(request, token): """ Preview file via shared link. """ assert token is not None # Checked by URLconf try: fileshare = FileShare.objects.get(token=token) except FileShare.DoesNotExist: raise Http404 shared_by = fileshare.username repo_id = fileshare.repo_id repo = get_repo(repo_id) if not repo: raise Http404 path = fileshare.path.rstrip('/') # Normalize file path obj_id = seafile_api.get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u'File does not exist')) file_size = seafile_api.get_file_size(obj_id) filename = os.path.basename(path) filetype, fileext = get_file_type_and_ext(filename) access_token = seafserv_rpc.web_get_access_token(repo.id, obj_id, 'view', '') raw_path = gen_file_get_url(access_token, filename) inner_path = gen_inner_file_get_url(access_token, filename) # get file content ret_dict = {'err': '', 'file_content': '', 'encoding': '', 'file_enc': '', 'file_encoding_list': [], 'html_exists': False, 'filetype': filetype} fsize = get_file_size(obj_id) exceeds_limit, err_msg = file_size_exceeds_preview_limit(fsize, filetype) if exceeds_limit: err = err_msg else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, inner_path, ret_dict) elif filetype == DOCUMENT: handle_document(inner_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(inner_path, obj_id, fileext, ret_dict) # Increase file shared link view_cnt, this operation should be atomic fileshare.view_cnt = F('view_cnt') + 1 fileshare.save() # send statistic messages if ret_dict['filetype'] != 'Unknown': try: obj_size = seafserv_threaded_rpc.get_file_size(obj_id) send_message('seahub.stats', 'file-view\t%s\t%s\t%s\t%s' % \ (repo.id, shared_by, obj_id, obj_size)) except SearpcError, e: logger.error('Error when sending file-view message: %s' % str(e))
def post(self, request): license_file = request.FILES.get('license', None) if not license_file: error_msg = 'license can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(license_file.name) if ext != 'txt': error_msg = file_type_error_msg(ext, 'txt') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if license_file.size > 1024 * 1024 * 5: # 5mb error_msg = file_size_error_msg(license_file.size, 5*1024*1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) license_dir = os.path.dirname(LICENSE_PATH) try: if not os.path.exists(license_dir): error_msg = 'path %s invalid.' % LICENSE_PATH return api_error(status.HTTP_400_BAD_REQUEST, error_msg) with open(LICENSE_PATH, 'w') as fd: fd.write(license_file.read()) ccnet_api.reload_license() except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True}, status=status.HTTP_200_OK)
def render_file_revisions (request, repo_id): """List all history versions of a file.""" days_str = request.GET.get('days', '') try: days = int(days_str) except ValueError: days = 7 path = request.GET.get('p', '/') if path[-1] == '/': path = path[:-1] u_filename = os.path.basename(path) if not path: return render_error(request) repo = get_repo(repo_id) if not repo: error_msg = _(u"Library does not exist") return render_error(request, error_msg) filetype = get_file_type_and_ext(u_filename)[0].lower() if filetype == 'text' or filetype == 'markdown': can_compare = True else: can_compare = False try: commits = seafile_api.get_file_revisions(repo_id, path, -1, -1, days) except SearpcError, e: logger.error(e.msg) return render_error(request, e.msg)
def get_onlyoffice_dict(username, repo_id, file_path, file_id='', can_edit=False, can_download=True): repo = seafile_api.get_repo(repo_id) if repo.is_virtual: origin_repo_id = repo.origin_repo_id origin_file_path = posixpath.join(repo.origin_path, file_path.strip('/')) # for view history/trash/snapshot file if not file_id: file_id = seafile_api.get_file_id_by_path(origin_repo_id, origin_file_path) else: origin_repo_id = repo_id origin_file_path = file_path if not file_id: file_id = seafile_api.get_file_id_by_path(repo_id, file_path) dl_token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'download', username, use_onetime=True) if not dl_token: return None filetype, fileext = get_file_type_and_ext(file_path) if fileext in ('xls', 'xlsx', 'ods', 'fods', 'csv'): document_type = 'spreadsheet' elif fileext in ('pptx', 'ppt', 'odp', 'fodp', 'ppsx', 'pps'): document_type = 'presentation' else: document_type = 'text' doc_info = json.dumps({'repo_id': repo_id, 'file_path': file_path, 'username': username}) doc_key = hashlib.md5(force_bytes(origin_repo_id + origin_file_path + file_id)).hexdigest()[:20] cache.set("ONLYOFFICE_%s" % doc_key, doc_info, None) file_name = os.path.basename(file_path.rstrip('/')) doc_url = gen_file_get_url(dl_token, file_name) base_url = get_site_scheme_and_netloc() onlyoffice_editor_callback_url = reverse('onlyoffice_editor_callback') calllback_url = urlparse.urljoin(base_url, onlyoffice_editor_callback_url) return_dict = { 'repo_id': repo_id, 'path': file_path, 'ONLYOFFICE_APIJS_URL': ONLYOFFICE_APIJS_URL, 'file_type': fileext, 'doc_key': doc_key, 'doc_title': file_name, 'doc_url': doc_url, 'document_type': document_type, 'callback_url': calllback_url, 'can_edit': can_edit, 'can_download': can_download, 'username': username, 'enable_watermark': ENABLE_WATERMARK and not can_edit, } return return_dict
def get_group_msgs(groupid, page, username): # Show 15 group messages per page. paginator = Paginator(GroupMessage.objects.filter(group_id=groupid).order_by("-timestamp"), 15) # If page request (9999) is out of range, return None try: group_msgs = paginator.page(page) except (EmptyPage, InvalidPage): return None # Force evaluate queryset to fix some database error for mysql. group_msgs.object_list = list(group_msgs.object_list) attachments = MessageAttachment.objects.filter(group_message__in=group_msgs.object_list) msg_replies = MessageReply.objects.filter(reply_to__in=group_msgs.object_list) reply_to_list = [r.reply_to_id for r in msg_replies] for msg in group_msgs.object_list: msg.reply_cnt = reply_to_list.count(msg.id) msg.replies = [] for r in msg_replies: if msg.id == r.reply_to_id: msg.replies.append(r) msg.replies = msg.replies[-3:] for att in attachments: if att.group_message_id != msg.id: continue # Attachment name is file name or directory name. # If is top directory, use repo name instead. path = att.path if path == "/": repo = seafile_api.get_repo(att.repo_id) if not repo: # TODO: what should we do here, tell user the repo # is no longer exists? continue att.name = repo.name else: path = path.rstrip("/") # cut out last '/' if possible att.name = os.path.basename(path) # Load to discuss page if attachment is a image and from recommend. if att.attach_type == "file" and att.src == "recommend": att.filetype, att.fileext = get_file_type_and_ext(att.name) if att.filetype == IMAGE: att.obj_id = seafile_api.get_file_id_by_path(att.repo_id, path) if not att.obj_id: att.err = "File does not exist" else: att.token = seafile_api.get_fileserver_access_token(att.repo_id, att.obj_id, "view", username) att.img_url = gen_file_get_url(att.token, att.name) msg.attachment = att return group_msgs
def view_file_via_shared_dir(request, token): assert token is not None # Checked by URLconf try: fileshare = FileShare.objects.get(token=token) except FileShare.DoesNotExist: raise Http404 shared_by = fileshare.username repo_id = fileshare.repo_id repo = get_repo(repo_id) if not repo: raise Http404 path = request.GET.get('p', '').rstrip('/') if not path: raise Http404 if not path.startswith(fileshare.path): # Can not view upper dir of shared dir raise Http404 zipped = gen_path_link(path, '') obj_id = seafile_api.get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u'File does not exist')) file_size = seafile_api.get_file_size(obj_id) filename = os.path.basename(path) filetype, fileext = get_file_type_and_ext(filename) access_token = seafserv_rpc.web_get_access_token(repo.id, obj_id, 'view', '') raw_path = gen_file_get_url(access_token, filename) inner_path = gen_inner_file_get_url(access_token, filename) # get file content ret_dict = {'err': '', 'file_content': '', 'encoding': '', 'file_enc': '', 'file_encoding_list': [], 'html_exists': False, 'filetype': filetype} fsize = get_file_size(obj_id) exceeds_limit, err_msg = file_size_exceeds_preview_limit(fsize, filetype) if exceeds_limit: err = err_msg else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, inner_path, ret_dict) elif filetype == DOCUMENT: handle_document(inner_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(inner_path, obj_id, fileext, ret_dict) # send statistic messages try: obj_size = seafserv_threaded_rpc.get_file_size(obj_id) send_message('seahub.stats', 'file-view\t%s\t%s\t%s\t%s' % \ (repo.id, shared_by, obj_id, obj_size)) except SearpcError, e: logger.error('Error when sending file-view message: %s' % str(e))
def allow_generate_thumbnail(username, repo, f): # check if thumbnail is allowed if seafile_api.check_repo_access_permission(repo.id, username) is None: # user can not access repo return False file_type, file_ext = get_file_type_and_ext(f.obj_name) if not repo.encrypted and file_type == IMAGE and ENABLE_THUMBNAIL \ and f.file_size < THUMBNAIL_IMAGE_SIZE_LIMIT * 1024**2: return True else: return False
def repl(matchobj): if matchobj.group(2): # return origin string in backquotes return matchobj.group(2) page_alias = page_name = matchobj.group(1).strip() if len(page_name.split("|")) > 1: page_alias = page_name.split("|")[0] page_name = page_name.split("|")[1] filetype, fileext = get_file_type_and_ext(page_name) if fileext == "": # convert page_name that extension is missing to a markdown page try: dirent = get_wiki_dirent(repo_id, page_name) a_tag = "<a href='%s'>%s</a>" return a_tag % (smart_str(url_prefix + normalize_page_name(page_name) + "/"), page_alias) except (WikiDoesNotExist, WikiPageMissing): a_tag = """<a class="wiki-page-missing" href='%s'>%s</a>""" return a_tag % (smart_str(url_prefix + page_name.replace("/", "-") + "/"), page_alias) elif filetype == IMAGE: # load image to wiki page path = "/" + page_name filename = os.path.basename(path) obj_id = seaserv.get_file_id_by_path(repo_id, path) if not obj_id: # Replace '/' in page_name to '-', since wiki name can not # contain '/'. return """<a class="wiki-page-missing" href='%s'>%s</a>""" % ( url_prefix + "/" + page_name.replace("/", "-"), page_name, ) token = seaserv.web_get_access_token(repo_id, obj_id, "view", username) ret = '<img class="wiki-image" src="%s" alt="%s" />' % (gen_file_get_url(token, filename), filename) return smart_str(ret) else: from seahub.base.templatetags.seahub_tags import file_icon_filter from django.conf import settings # convert other types of filelinks to clickable links path = "/" + page_name icon = file_icon_filter(page_name) s = reverse("repo_view_file", args=[repo_id]) + "?p=" + urlquote(path) a_tag = ( """<img src="%simg/file/%s" alt="%s" class="vam" /> <a href='%s' target='_blank' class="vam">%s</a>""" ) ret = a_tag % (settings.MEDIA_URL, icon, icon, smart_str(s), page_name) return smart_str(ret)
def post(self, request): favicon_file = request.FILES.get('favicon', None) if not favicon_file: error_msg = 'Favicon can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(favicon_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get(IMAGE)) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if favicon_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(favicon_file.size, 20*1024*1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: custom_favicon_file = os.path.join(SEAHUB_DATA_ROOT, CUSTOM_FAVICON_PATH) # save favicon file to custom dir with open(custom_favicon_file, 'w') as fd: fd.write(favicon_file.read()) custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_FAVICON_PATH)) # create symlink for custom dir if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def repl(matchobj): if matchobj.group(2): # return origin string in backquotes return matchobj.group(2) page_alias = page_name = matchobj.group(1).strip() if len(page_name.split('|')) > 1: page_alias = page_name.split('|')[0] page_name = page_name.split('|')[1] filetype, fileext = get_file_type_and_ext(page_name) if fileext == '': # convert page_name that extension is missing to a markdown page try: dirent = get_wiki_dirent(repo_id, page_name) a_tag = '''<a href="%s">%s</a>''' return a_tag % (smart_str(url_prefix + normalize_page_name(page_name) + '/'), page_alias) except (WikiDoesNotExist, WikiPageMissing): a_tag = '''<a href="%s" class="wiki-page-missing">%s</a>''' return a_tag % (smart_str(url_prefix + normalize_page_name(page_name) + '/'), page_alias) elif filetype == IMAGE: # load image to wiki page path = "/" + page_name filename = os.path.basename(path) obj_id = seaserv.get_file_id_by_path(repo_id, path) if not obj_id: # Replace '/' in page_name to '-', since wiki name can not # contain '/'. return '''<a href="%s" class="wiki-page-missing">%s</a>''' % \ (url_prefix + '/' + page_name.replace('/', '-'), page_name) token = seafile_api.get_fileserver_access_token(repo_id, obj_id, 'view', username) ret = '<img src="%s" alt="%s" class="wiki-image" />' % (gen_file_get_url(token, filename), filename) return smart_str(ret) else: from seahub.base.templatetags.seahub_tags import file_icon_filter from django.conf import settings # convert other types of filelinks to clickable links path = "/" + page_name icon = file_icon_filter(page_name) s = reverse('view_lib_file', args=[repo_id, urlquote(path)]) a_tag = '''<img src="%simg/file/%s" alt="%s" class="file-icon vam" /> <a href="%s" class="vam" target="_blank">%s</a>''' ret = a_tag % (settings.MEDIA_URL, icon, icon, smart_str(s), page_name) return smart_str(ret)
def allow_generate_thumbnail(request, repo_id, path): """check if thumbnail is allowed """ # get file type obj_name = os.path.basename(path) file_type, file_ext = get_file_type_and_ext(obj_name) # get file size file_id = get_file_id_by_path(repo_id, path) if not file_id: return False repo = get_repo(repo_id) file_size = get_file_size(repo.store_id, repo.version, file_id) if repo.encrypted or file_type != IMAGE or not ENABLE_THUMBNAIL: return False # check image compressed size limit if file_size < THUMBNAIL_IMAGE_COMPRESSED_SIZE_LIMIT * 1024**2: return True # get image memory cost token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'view', '', use_onetime = True) inner_path = gen_inner_file_get_url(token, obj_name) try: image_file = urllib2.urlopen(inner_path) f = StringIO(image_file.read()) image = Image.open(f) width, height = image.size # check image memory cost size limit # use RGBA as default mode(4x8-bit pixels, true colour with transparency mask) # every pixel will cost 4 byte in RGBA mode image_memory_cost = width * height * 4 / 1024 / 1024 if image_memory_cost < THUMBNAIL_IMAGE_ORIGINAL_SIZE_LIMIT: return True except Exception as e: logger.error(e) return False
def post(self, request): image_file = request.FILES.get('login_bg_image', None) if not image_file: error_msg = 'Image can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) file_type, ext = get_file_type_and_ext(image_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get('Image')) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if image_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(image_file.size, 20*1024*1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) custom_login_bg_image_path = get_custom_login_bg_image_path() custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(custom_login_bg_image_path)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: custom_login_bg_image_file = os.path.join(SEAHUB_DATA_ROOT, custom_login_bg_image_path) # save login background image file to custom dir with open(custom_login_bg_image_file, 'w') as fd: fd.write(image_file.read()) custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(custom_login_bg_image_path)) # create symlink for custom dir if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def repl(matchobj): if matchobj.group(2): # return origin string in backquotes return matchobj.group(2) link_alias = link_name = matchobj.group(1).strip() if len(link_name.split("|")) > 1: link_alias = link_name.split("|")[0] link_name = link_name.split("|")[1] filetype, fileext = get_file_type_and_ext(link_name) if fileext == "": # convert link_name that extension is missing to a markdown page try: dirent = get_wiki_dirent(repo_id, link_name) path = "/" + dirent.obj_name href = reverse("repo_view_file", args=[repo_id]) + "?p=" + urlquote(path) a_tag = """<a href="%s">%s</a>""" return a_tag % (href, link_alias) except (WikiDoesNotExist, WikiPageMissing): a_tag = """<p class="wiki-page-missing">%s</p>""" return a_tag % (link_alias) elif filetype == IMAGE: # load image to current page path = "/" + link_name filename = os.path.basename(path) obj_id = get_file_id_by_path(repo_id, path) if not obj_id: return """<p class="wiki-page-missing">%s</p>""" % link_name token = web_get_access_token(repo_id, obj_id, "view", username) return '<img class="wiki-image" src="%s" alt="%s" />' % (gen_file_get_url(token, filename), filename) else: from seahub.base.templatetags.seahub_tags import file_icon_filter # convert other types of filelinks to clickable links path = "/" + link_name icon = file_icon_filter(link_name) s = reverse("repo_view_file", args=[repo_id]) + "?p=" + urlquote(path) a_tag = ( """<img src="%simg/file/%s" alt="%s" class="vam" /> <a href="%s" target="_blank" class="vam">%s</a>""" ) return a_tag % (MEDIA_URL, icon, icon, s, link_name)
def post(self, request): logo_file = request.FILES.get('logo', None) if not logo_file: error_msg = 'Logo can not be found.' logger.error(error_msg) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(logo_file.name) if file_type != IMAGE: error_msg = file_type_error_msg(ext, PREVIEW_FILEEXT.get(IMAGE)) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if logo_file.size > 1024 * 1024 * 20: # 20mb error_msg = file_size_error_msg(logo_file.size, 20*1024*1024) return api_error(status.HTTP_400_BAD_REQUEST, error_msg) if not os.path.exists(SEAHUB_DATA_ROOT): os.makedirs(SEAHUB_DATA_ROOT) custom_dir = os.path.join(SEAHUB_DATA_ROOT, os.path.dirname(CUSTOM_LOGO_PATH)) if not os.path.exists(custom_dir): os.makedirs(custom_dir) try: # save logo file to custom dir custom_logo_file = os.path.join(SEAHUB_DATA_ROOT, CUSTOM_LOGO_PATH) image = Image.open(logo_file) image.save(custom_logo_file) # create symlink for custom dir custom_symlink = os.path.join(MEDIA_ROOT, os.path.dirname(CUSTOM_LOGO_PATH)) if not os.path.exists(custom_symlink): os.symlink(custom_dir, custom_symlink) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) return Response({'success': True})
def repl(matchobj): if matchobj.group(2): # return origin string in backquotes return matchobj.group(2) linkname = matchobj.group(1).strip() filetype, fileext = get_file_type_and_ext(linkname) filetype = filetype.lower() if fileext == '': # convert linkname that extension is missing to a markdown page filename = linkname + ".md" path = "/" + filename if get_file_id_by_path(repo_id, path): a_tag = "<a href='%s'>%s</a>" return a_tag % (reverse('group_wiki', args=[group.id, linkname]), linkname) else: a_tag = '''<a class="wiki-page-missing" href='%s'>%s</a>''' return a_tag % (reverse('group_wiki', args=[group.id, linkname.replace('/', '-')]), linkname) elif filetype == 'image': # load image to wiki page path = "/" + linkname filename = os.path.basename(path) obj_id = get_file_id_by_path(repo_id, path) if not obj_id: # Replace '/' in linkname to '-', since wiki name can not # contain '/'. return '''<a class="wiki-page-missing" href='%s'>%s</a>''' % \ (reverse('group_wiki', args=[group.id, linkname.replace('/', '-')]), linkname) token = web_get_access_token(repo_id, obj_id, 'view', username) return '<img src="%s" alt="%s" />' % (gen_file_get_url(token, filename), filename) else: from base.templatetags.seahub_tags import file_icon_filter # convert other types of filelinks to clickable links path = "/" + linkname icon = file_icon_filter(linkname) s = reverse('repo_view_file', args=[repo_id]) + '?p=' + path a_tag = '''<img src="%simg/file/%s" alt="%s" class="vam" /> <a href='%s' target='_blank' class="vam">%s</a>''' return a_tag % (MEDIA_URL, icon, icon, s, linkname)
def repl(matchobj): if matchobj.group(2): # return origin string in backquotes return matchobj.group(2) page_name = matchobj.group(1).strip() filetype, fileext = get_file_type_and_ext(page_name) if fileext == '': # convert page_name that extension is missing to a markdown page dirent = get_wiki_dirent(repo_id, page_name) if dirent is not None: a_tag = "<a href='%s'>%s</a>" return a_tag % (url_prefix + '/' + normalize_page_name(page_name), page_name) else: a_tag = '''<a class="wiki-page-missing" href='%s'>%s</a>''' return a_tag % (url_prefix + '/' + page_name.replace('/', '-'), page_name) elif filetype == IMAGE: # load image to wiki page path = "/" + page_name filename = os.path.basename(path) obj_id = get_file_id_by_path(repo_id, path) if not obj_id: # Replace '/' in page_name to '-', since wiki name can not # contain '/'. return '''<a class="wiki-page-missing" href='%s'>%s</a>''' % \ (url_prefix + '/' + page_name.replace('/', '-'), page_name) token = seaserv.web_get_access_token(repo_id, obj_id, 'view', username) return '<img src="%s" alt="%s" />' % (gen_file_get_url(token, filename), filename) else: from base.templatetags.seahub_tags import file_icon_filter # convert other types of filelinks to clickable links path = "/" + page_name icon = file_icon_filter(page_name) s = reverse('repo_view_file', args=[repo_id]) + \ '?p=' + urllib2.quote(smart_str(path)) a_tag = '''<img src="%simg/file/%s" alt="%s" class="vam" /> <a href='%s' target='_blank' class="vam">%s</a>''' return a_tag % (MEDIA_URL, icon, icon, s, page_name)
def view_file(request, repo_id): """ Steps to view file: 1. Get repo id and file path. 2. Check user's permission. 3. Check whether this file can be viewed online. 4.1 Get file content if file is text file. 4.2 Prepare flash if file is document. 4.3 Prepare or use pdfjs if file is pdf. 4.4 Other file return it's raw path. """ username = request.user.username # check arguments repo = get_repo(repo_id) if not repo: raise Http404 path = request.GET.get('p', '/') obj_id = get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u'File does not exist')) # construct some varibles u_filename = os.path.basename(path) current_commit = get_commits(repo_id, 0, 1)[0] # Check whether user has permission to view file and get file raw path, # render error page if permission is deny. raw_path, user_perm = get_file_view_path_and_perm(request, repo_id, obj_id, u_filename) if not user_perm: return render_permission_error(request, _(u'Unable to view file')) # get file type and extension filetype, fileext = get_file_type_and_ext(u_filename) img_prev = None img_next = None ret_dict = {'err': '', 'file_content': '', 'encoding': '', 'file_enc': '', 'file_encoding_list': [], 'html_exists': False, 'filetype': filetype} fsize = get_file_size(obj_id) exceeds_limit, err_msg = file_size_exceeds_preview_limit(fsize, filetype) if exceeds_limit: ret_dict['err'] = err_msg else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, raw_path, ret_dict) if filetype == MARKDOWN: c = ret_dict['file_content'] ret_dict['file_content'] = convert_md_link(c, repo_id, username) elif filetype == DOCUMENT: handle_document(raw_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(raw_path, obj_id, fileext, ret_dict) elif filetype == IMAGE: parent_dir = os.path.dirname(path) dirs = list_dir_by_path(current_commit.id, parent_dir) if not dirs: raise Http404 img_list = [] for dirent in dirs: if not stat.S_ISDIR(dirent.props.mode): fltype, flext = get_file_type_and_ext(dirent.obj_name) if fltype == 'Image': img_list.append(dirent.obj_name) if len(img_list) > 1: img_list.sort(lambda x, y : cmp(x.lower(), y.lower())) cur_img_index = img_list.index(u_filename) if cur_img_index != 0: img_prev = os.path.join(parent_dir, img_list[cur_img_index - 1]) if cur_img_index != len(img_list) - 1: img_next = os.path.join(parent_dir, img_list[cur_img_index + 1]) else: pass # generate file path navigator zipped = gen_path_link(path, repo.name) # file shared link l = FileShare.objects.filter(repo_id=repo_id).filter( username=username).filter(path=path) fileshare = l[0] if len(l) > 0 else None http_or_https = request.is_secure() and 'https' or 'http' domain = RequestSite(request).domain if fileshare: file_shared_link = gen_shared_link(request, fileshare.token, 'f') else: file_shared_link = '' # my contacts used in shared link autocomplete contacts = Contact.objects.filter(user_email=username) """List repo groups""" # Get groups this repo is shared. if request.user.org: org_id = request.user.org['org_id'] repo_shared_groups = get_org_groups_by_repo(org_id, repo_id) else: repo_shared_groups = get_shared_groups_by_repo(repo_id) # Filter out groups that user in joined. groups = [ x for x in repo_shared_groups if is_group_user(x.id, username)] if len(groups) > 1: ctx = {} ctx['groups'] = groups repogrp_str = render_to_string("snippets/repo_group_list.html", ctx) else: repogrp_str = '' file_path_hash = md5_constructor(urllib2.quote(path.encode('utf-8'))).hexdigest()[:12] # fetch file contributors and latest contributor contributors, last_modified, last_commit_id = get_file_contributors(repo_id, path.encode('utf-8'), file_path_hash, obj_id) latest_contributor = contributors[0] if contributors else None # check whether file is starred is_starred = False org_id = -1 if request.user.org: org_id = request.user.org['org_id'] is_starred = is_file_starred(username, repo.id, path.encode('utf-8'), org_id) template = 'view_file_%s.html' % ret_dict['filetype'].lower() search_repo_id = None if not repo.encrypted: search_repo_id = repo.id return render_to_response(template, { 'repo': repo, 'obj_id': obj_id, 'filename': u_filename, 'path': path, 'zipped': zipped, 'current_commit': current_commit, 'fileext': fileext, 'raw_path': raw_path, 'fileshare': fileshare, 'protocol': http_or_https, 'domain': domain, 'file_shared_link': file_shared_link, 'contacts': contacts, 'err': ret_dict['err'], 'file_content': ret_dict['file_content'], 'file_enc': ret_dict['file_enc'], 'encoding': ret_dict['encoding'], 'file_encoding_list':ret_dict['file_encoding_list'], 'html_exists': ret_dict['html_exists'], 'html_detail': ret_dict.get('html_detail', {}), 'filetype': ret_dict['filetype'], "applet_root": get_ccnetapplet_root(), 'groups': groups, 'use_pdfjs':USE_PDFJS, 'contributors': contributors, 'latest_contributor': latest_contributor, 'last_modified': last_modified, 'last_commit_id': last_commit_id, 'repo_group_str': repogrp_str, 'is_starred': is_starred, 'user_perm': user_perm, 'img_prev': img_prev, 'img_next': img_next, 'search_repo_id': search_repo_id, }, context_instance=RequestContext(request))
def view_shared_dir(request, token): assert token is not None # Checked by URLconf fileshare = FileShare.objects.get_valid_dir_link_by_token(token) if fileshare is None: raise Http404 if fileshare.is_encrypted(): if not check_share_link_access(request, token): d = {'token': token, 'view_name': 'view_shared_dir', } if request.method == 'POST': post_values = request.POST.copy() post_values['enc_password'] = fileshare.password form = SharedLinkPasswordForm(post_values) d['form'] = form if form.is_valid(): set_share_link_access(request, token) else: return render_to_response('share_access_validation.html', d, context_instance=RequestContext(request)) else: return render_to_response('share_access_validation.html', d, context_instance=RequestContext(request)) username = fileshare.username repo_id = fileshare.repo_id # Get path from frontend, use '/' if missing, and construct request path # with fileshare.path to real path, used to fetch dirents by RPC. req_path = request.GET.get('p', '/') if req_path[-1] != '/': req_path += '/' if req_path == '/': real_path = fileshare.path else: real_path = posixpath.join(fileshare.path, req_path.lstrip('/')) if real_path[-1] != '/': # Normalize dir path real_path += '/' repo = get_repo(repo_id) if not repo: raise Http404 # Check path still exist, otherwise show error if not seafile_api.get_dir_id_by_path(repo.id, fileshare.path): return render_error(request, _('"%s" does not exist.') % fileshare.path) # download shared dir if request.GET.get('dl', '') == '1': return _download_dir_from_share_link(request, fileshare, repo, real_path) if fileshare.path == '/': # use repo name as dir name if share whole library dir_name = repo.name else: dir_name = os.path.basename(real_path[:-1]) current_commit = seaserv.get_commits(repo_id, 0, 1)[0] file_list, dir_list, dirent_more = get_repo_dirents(request, repo, current_commit, real_path) # generate dir navigator if fileshare.path == '/': zipped = gen_path_link(req_path, repo.name) else: zipped = gen_path_link(req_path, os.path.basename(fileshare.path[:-1])) if req_path == '/': # When user view the root of shared dir.. # increase shared link view_cnt, fileshare = FileShare.objects.get(token=token) fileshare.view_cnt = F('view_cnt') + 1 fileshare.save() traffic_over_limit = user_traffic_over_limit(fileshare.username) for f in file_list: file_type, file_ext = get_file_type_and_ext(f.obj_name) if file_type == IMAGE: f.is_img = True if allow_generate_thumbnail(username, repo, f): f.allow_generate_thumbnail = True if os.path.exists(os.path.join(THUMBNAIL_ROOT, THUMBNAIL_DEFAULT_SIZE, f.obj_id)): f.thumbnail_src = get_thumbnail_src(repo.id, f.obj_id, THUMBNAIL_DEFAULT_SIZE) return render_to_response('view_shared_dir.html', { 'repo': repo, 'token': token, 'path': req_path, 'username': username, 'dir_name': dir_name, 'file_list': file_list, 'dir_list': dir_list, 'zipped': zipped, 'traffic_over_limit': traffic_over_limit, 'ENABLE_THUMBNAIL': ENABLE_THUMBNAIL, 'PREVIEW_DEFAULT_SIZE': PREVIEW_DEFAULT_SIZE, }, context_instance=RequestContext(request))
def view_file_via_shared_dir(request, token): assert token is not None # Checked by URLconf fileshare = FileShare.objects.get_valid_file_link_by_token(token) if fileshare is None: raise Http404 shared_by = fileshare.username repo_id = fileshare.repo_id repo = get_repo(repo_id) if not repo: raise Http404 path = request.GET.get('p', '').rstrip('/') if not path: raise Http404 if not path.startswith(fileshare.path): # Can not view upper dir of shared dir raise Http404 zipped = gen_path_link(path, '') obj_id = seafile_api.get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u'File does not exist')) file_size = seafile_api.get_file_size(repo.store_id, repo.version, obj_id) filename = os.path.basename(path) filetype, fileext = get_file_type_and_ext(filename) access_token = seafserv_rpc.web_get_access_token(repo.id, obj_id, 'view', '') raw_path = gen_file_get_url(access_token, filename) inner_path = gen_inner_file_get_url(access_token, filename) img_prev = None img_next = None # get file content ret_dict = {'err': '', 'file_content': '', 'encoding': '', 'file_enc': '', 'file_encoding_list': [], 'html_exists': False, 'filetype': filetype} exceeds_limit, err_msg = file_size_exceeds_preview_limit(file_size, filetype) if exceeds_limit: ret_dict['err'] = err_msg else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, inner_path, ret_dict) elif filetype == DOCUMENT: handle_document(inner_path, obj_id, fileext, ret_dict) elif filetype == SPREADSHEET: handle_spreadsheet(inner_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(inner_path, obj_id, fileext, ret_dict) elif filetype == IMAGE: current_commit = get_commits(repo_id, 0, 1)[0] parent_dir = os.path.dirname(path) dirs = seafile_api.list_dir_by_commit_and_path(current_commit.repo_id, current_commit.id, parent_dir) if not dirs: raise Http404 img_list = [] for dirent in dirs: if not stat.S_ISDIR(dirent.props.mode): fltype, flext = get_file_type_and_ext(dirent.obj_name) if fltype == 'Image': img_list.append(dirent.obj_name) if len(img_list) > 1: img_list.sort(lambda x, y : cmp(x.lower(), y.lower())) cur_img_index = img_list.index(filename) if cur_img_index != 0: img_prev = posixpath.join(parent_dir, img_list[cur_img_index - 1]) if cur_img_index != len(img_list) - 1: img_next = posixpath.join(parent_dir, img_list[cur_img_index + 1]) # send statistic messages if ret_dict['filetype'] != 'Unknown': try: send_message('seahub.stats', 'file-view\t%s\t%s\t%s\t%s' % \ (repo.id, shared_by, obj_id, file_size)) except SearpcError, e: logger.error('Error when sending file-view message: %s' % str(e))
def view_shared_file(request, token): """ Preview file via shared link. """ assert token is not None # Checked by URLconf fileshare = FileShare.objects.get_valid_file_link_by_token(token) if fileshare is None: raise Http404 if fileshare.is_encrypted(): if not check_share_link_access(request.user.username, token): d = {'token': token, 'view_name': 'view_shared_file', } if request.method == 'POST': post_values = request.POST.copy() post_values['enc_password'] = fileshare.password form = SharedLinkPasswordForm(post_values) d['form'] = form if form.is_valid(): # set cache for non-anonymous user if request.user.is_authenticated(): set_share_link_access(request.user.username, token) else: return render_to_response('share_access_validation.html', d, context_instance=RequestContext(request)) else: return render_to_response('share_access_validation.html', d, context_instance=RequestContext(request)) shared_by = fileshare.username repo_id = fileshare.repo_id repo = get_repo(repo_id) if not repo: raise Http404 path = fileshare.path.rstrip('/') # Normalize file path obj_id = seafile_api.get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u'File does not exist')) file_size = seafile_api.get_file_size(repo.store_id, repo.version, obj_id) filename = os.path.basename(path) filetype, fileext = get_file_type_and_ext(filename) access_token = seafserv_rpc.web_get_access_token(repo.id, obj_id, 'view', '') raw_path = gen_file_get_url(access_token, filename) inner_path = gen_inner_file_get_url(access_token, filename) # get file content ret_dict = {'err': '', 'file_content': '', 'encoding': '', 'file_enc': '', 'file_encoding_list': [], 'html_exists': False, 'filetype': filetype} exceeds_limit, err_msg = file_size_exceeds_preview_limit(file_size, filetype) if exceeds_limit: ret_dict['err'] = err_msg else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, inner_path, ret_dict) elif filetype == DOCUMENT: handle_document(inner_path, obj_id, fileext, ret_dict) elif filetype == SPREADSHEET: handle_spreadsheet(inner_path, obj_id, fileext, ret_dict) elif filetype == OPENDOCUMENT: if file_size == 0: ret_dict['err'] = _(u'Invalid file format.') elif filetype == PDF: handle_pdf(inner_path, obj_id, fileext, ret_dict) # Increase file shared link view_cnt, this operation should be atomic fileshare.view_cnt = F('view_cnt') + 1 fileshare.save() # send statistic messages if ret_dict['filetype'] != 'Unknown': try: send_message('seahub.stats', 'file-view\t%s\t%s\t%s\t%s' % \ (repo.id, shared_by, obj_id, file_size)) except SearpcError, e: logger.error('Error when sending file-view message: %s' % str(e))
def view_history_file_common(request, repo_id, ret_dict): username = request.user.username # check arguments repo = get_repo(repo_id) if not repo: raise Http404 path = request.GET.get("p", "/") commit_id = request.GET.get("commit_id", "") if not commit_id: raise Http404 obj_id = request.GET.get("obj_id", "") if not obj_id: raise Http404 # construct some varibles u_filename = os.path.basename(path) filename_utf8 = urllib2.quote(u_filename.encode("utf-8")) current_commit = get_commit(commit_id) if not current_commit: raise Http404 # Check whether user has permission to view file and get file raw path, # render error page if permission is deny. raw_path, user_perm = get_file_view_path_and_perm(request, repo_id, obj_id, u_filename) request.user_perm = user_perm # get file type and extension filetype, fileext = get_file_type_and_ext(u_filename) if user_perm: # Check file size fsize = get_file_size(obj_id) if fsize > FILE_PREVIEW_MAX_SIZE: from django.template.defaultfilters import filesizeformat err = _(u"File size surpasses %s, can not be opened online.") % filesizeformat(FILE_PREVIEW_MAX_SIZE) ret_dict["err"] = err else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, raw_path, ret_dict) elif filetype == DOCUMENT: handle_document(raw_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(raw_path, obj_id, fileext, ret_dict) else: pass # populate return value dict ret_dict["repo"] = repo ret_dict["obj_id"] = obj_id ret_dict["file_name"] = u_filename ret_dict["path"] = path ret_dict["current_commit"] = current_commit ret_dict["fileext"] = fileext ret_dict["raw_path"] = raw_path if not ret_dict.has_key("filetype"): ret_dict["filetype"] = filetype ret_dict["DOCUMENT_CONVERTOR_ROOT"] = DOCUMENT_CONVERTOR_ROOT ret_dict["use_pdfjs"] = USE_PDFJS
def view_shared_dir(request, fileshare): token = fileshare.token password_check_passed, err_msg = check_share_link_common(request, fileshare) if not password_check_passed: d = {'token': token, 'view_name': 'view_shared_dir', 'err_msg': err_msg} return render_to_response('share_access_validation.html', d, context_instance=RequestContext(request)) username = fileshare.username repo_id = fileshare.repo_id # Get path from frontend, use '/' if missing, and construct request path # with fileshare.path to real path, used to fetch dirents by RPC. req_path = request.GET.get('p', '/') if req_path[-1] != '/': req_path += '/' if req_path == '/': real_path = fileshare.path else: real_path = posixpath.join(fileshare.path, req_path.lstrip('/')) if real_path[-1] != '/': # Normalize dir path real_path += '/' repo = get_repo(repo_id) if not repo: raise Http404 # Check path still exist, otherwise show error if not seafile_api.get_dir_id_by_path(repo.id, fileshare.path): return render_error(request, _('"%s" does not exist.') % fileshare.path) if fileshare.path == '/': # use repo name as dir name if share whole library dir_name = repo.name else: dir_name = os.path.basename(real_path[:-1]) current_commit = seaserv.get_commits(repo_id, 0, 1)[0] file_list, dir_list, dirent_more = get_repo_dirents(request, repo, current_commit, real_path) # generate dir navigator if fileshare.path == '/': zipped = gen_path_link(req_path, repo.name) else: zipped = gen_path_link(req_path, os.path.basename(fileshare.path[:-1])) if req_path == '/': # When user view the root of shared dir.. # increase shared link view_cnt, fileshare = FileShare.objects.get(token=token) fileshare.view_cnt = F('view_cnt') + 1 fileshare.save() traffic_over_limit = user_traffic_over_limit(fileshare.username) # mode to view dir/file items mode = request.GET.get('mode', 'list') if mode != 'list': mode = 'grid' thumbnail_size = THUMBNAIL_DEFAULT_SIZE if mode == 'list' else THUMBNAIL_SIZE_FOR_GRID if not repo.encrypted and ENABLE_THUMBNAIL: for f in file_list: file_type, file_ext = get_file_type_and_ext(f.obj_name) if file_type == IMAGE: f.is_img = True if os.path.exists(os.path.join(THUMBNAIL_ROOT, str(thumbnail_size), f.obj_id)): req_image_path = posixpath.join(req_path, f.obj_name) src = get_share_link_thumbnail_src(token, thumbnail_size, req_image_path) f.encoded_thumbnail_src = urlquote(src) return render_to_response('view_shared_dir.html', { 'repo': repo, 'token': token, 'path': req_path, 'username': username, 'dir_name': dir_name, 'file_list': file_list, 'dir_list': dir_list, 'zipped': zipped, 'traffic_over_limit': traffic_over_limit, 'ENABLE_THUMBNAIL': ENABLE_THUMBNAIL, 'mode': mode, 'thumbnail_size': thumbnail_size, }, context_instance=RequestContext(request))
def get_group_msgs(groupid, page, username): # Show 15 group messages per page. paginator = Paginator( GroupMessage.objects.filter(group_id=groupid).order_by('-timestamp'), 15) # If page request (9999) is out of range, return None try: group_msgs = paginator.page(page) except (EmptyPage, InvalidPage): return None # Force evaluate queryset to fix some database error for mysql. group_msgs.object_list = list(group_msgs.object_list) attachments = MessageAttachment.objects.filter( group_message__in=group_msgs.object_list) msg_replies = MessageReply.objects.filter( reply_to__in=group_msgs.object_list) reply_to_list = [r.reply_to_id for r in msg_replies] for msg in group_msgs.object_list: msg.reply_cnt = reply_to_list.count(msg.id) msg.replies = [] for r in msg_replies: if msg.id == r.reply_to_id: msg.replies.append(r) msg.replies = msg.replies[-3:] for att in attachments: if att.group_message_id != msg.id: continue # Attachment name is file name or directory name. # If is top directory, use repo name instead. path = att.path if path == '/': repo = seafile_api.get_repo(att.repo_id) if not repo: # TODO: what should we do here, tell user the repo # is no longer exists? continue att.name = repo.name else: path = path.rstrip('/') # cut out last '/' if possible att.name = os.path.basename(path) # Load to discuss page if attachment is a image and from recommend. if att.attach_type == 'file' and att.src == 'recommend': att.filetype, att.fileext = get_file_type_and_ext(att.name) if att.filetype == IMAGE: att.obj_id = seafile_api.get_file_id_by_path( att.repo_id, path) if not att.obj_id: att.err = 'File does not exist' else: att.token = seafile_api.get_fileserver_access_token( att.repo_id, att.obj_id, 'view', username) att.img_url = gen_file_get_url(att.token, att.name) msg.attachment = att return group_msgs
def render_repo(request, repo): """Steps to show repo page: If user has permission to view repo If repo is encrypt and password is not set on server return decrypt repo page If repo is not encrypt or password is set on server Show repo direntries based on requested path If user does not have permission to view repo return permission deny page """ username = request.user.username path = get_path_from_request(request) if not seafile_api.get_dir_id_by_path(repo.id, path): raise Http404 user_perm = check_repo_access_permission(repo.id, request.user) if user_perm is None: return render_to_response('repo_access_deny.html', { 'repo': repo, }, context_instance=RequestContext(request)) sub_lib_enabled = UserOptions.objects.is_sub_lib_enabled(username) server_crypto = False if repo.encrypted: try: server_crypto = UserOptions.objects.is_server_crypto(username) except CryptoOptionNotSetError: return render_to_response('options/set_user_options.html', { }, context_instance=RequestContext(request)) if (repo.enc_version == 1 or (repo.enc_version == 2 and server_crypto)) \ and not is_password_set(repo.id, username): return render_to_response('decrypt_repo_form.html', { 'repo': repo, 'next': get_next_url_from_request(request) or reverse('repo', args=[repo.id]), 'force_server_crypto': FORCE_SERVER_CRYPTO, }, context_instance=RequestContext(request)) # query context args fileserver_root = get_fileserver_root() max_upload_file_size = get_max_upload_file_size() protocol = request.is_secure() and 'https' or 'http' domain = RequestSite(request).domain for g in request.user.joined_groups: g.avatar = grp_avatar(g.id, 20) head_commit = get_commit(repo.id, repo.version, repo.head_cmmt_id) if not head_commit: raise Http404 if new_merge_with_no_conflict(head_commit): info_commit = get_commit_before_new_merge(head_commit) else: info_commit = head_commit repo_size = get_repo_size(repo.id) no_quota = is_no_quota(repo.id) if is_org_context(request): repo_owner = seafile_api.get_org_repo_owner(repo.id) else: repo_owner = seafile_api.get_repo_owner(repo.id) is_repo_owner = True if repo_owner == username else False if is_repo_owner and not repo.is_virtual: show_repo_settings = True else: show_repo_settings = False more_start = None file_list, dir_list, dirent_more = get_repo_dirents(request, repo, head_commit, path, offset=0, limit=100) if dirent_more: more_start = 100 zipped = get_nav_path(path, repo.name) repo_groups = get_shared_groups_by_repo_and_user(repo.id, username) if len(repo_groups) > 1: repo_group_str = render_to_string("snippets/repo_group_list.html", {'groups': repo_groups}) else: repo_group_str = '' upload_url = get_upload_url(request, repo.id) fileshare = get_fileshare(repo.id, username, path) dir_shared_link = get_dir_share_link(fileshare) uploadlink = get_uploadlink(repo.id, username, path) dir_shared_upload_link = get_dir_shared_upload_link(uploadlink) if not repo.encrypted and ENABLE_THUMBNAIL: size = THUMBNAIL_DEFAULT_SIZE for f in file_list: file_type, file_ext = get_file_type_and_ext(f.obj_name) if file_type == IMAGE: f.is_img = True if os.path.exists(os.path.join(THUMBNAIL_ROOT, size, f.obj_id)): f.thumbnail_src = get_thumbnail_src(repo.id, f.obj_id, size) return render_to_response('repo.html', { 'repo': repo, 'user_perm': user_perm, 'repo_owner': repo_owner, 'is_repo_owner': is_repo_owner, 'show_repo_settings': show_repo_settings, 'current_commit': head_commit, 'info_commit': info_commit, 'password_set': True, 'repo_size': repo_size, 'dir_list': dir_list, 'file_list': file_list, 'dirent_more': dirent_more, 'more_start': more_start, 'path': path, 'zipped': zipped, 'groups': repo_groups, 'repo_group_str': repo_group_str, 'no_quota': no_quota, 'max_upload_file_size': max_upload_file_size, 'upload_url': upload_url, 'fileserver_root': fileserver_root, 'protocol': protocol, 'domain': domain, 'fileshare': fileshare, 'dir_shared_link': dir_shared_link, 'uploadlink': uploadlink, 'dir_shared_upload_link': dir_shared_upload_link, 'ENABLE_SUB_LIBRARY': ENABLE_SUB_LIBRARY, 'server_crypto': server_crypto, 'sub_lib_enabled': sub_lib_enabled, 'enable_upload_folder': ENABLE_UPLOAD_FOLDER, 'ENABLE_THUMBNAIL': ENABLE_THUMBNAIL, 'PREVIEW_DEFAULT_SIZE': PREVIEW_DEFAULT_SIZE, }, context_instance=RequestContext(request))
def post(self, request): """ Import users from xlsx file Permission checking: 1. admin user. """ xlsx_file = request.FILES.get('file', None) if not xlsx_file: error_msg = 'file can not be found.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_type, ext = get_file_type_and_ext(xlsx_file.name) if ext != 'xlsx': error_msg = file_type_error_msg(ext, 'xlsx') return api_error(status.HTTP_400_BAD_REQUEST, error_msg) content = xlsx_file.read() try: fs = BytesIO(content) wb = load_workbook(filename=fs, read_only=True) except Exception as e: logger.error(e) # example file is like: # Email Password Name(Optional) Role(Optional) Space Quota(MB, Optional) # [email protected] a a default 1024 # [email protected] b b default 2048 rows = wb.worksheets[0].rows records = [] # skip first row(head field). next(rows) for row in rows: records.append([col.value for col in row]) if user_number_over_limit(new_users=len(records)): error_msg = 'The number of users exceeds the limit.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) result = {} result['failed'] = [] result['success'] = [] for record in records: if record[0]: email = record[0].strip() if not is_valid_username(email): result['failed'].append({ 'email': email, 'error_msg': 'email %s invalid.' % email }) continue else: result['failed'].append({ 'email': '', 'error_msg': 'email invalid.' }) continue if record[1]: password = record[1].strip() if not password: result['failed'].append({ 'email': email, 'error_msg': 'password invalid.' }) continue else: result['failed'].append({ 'email': email, 'error_msg': 'password invalid.' }) continue try: User.objects.get(email=email) result['failed'].append({ 'email': email, 'error_msg': 'user %s exists.' % email }) continue except User.DoesNotExist: pass User.objects.create_user(email, password, is_staff=False, is_active=True) if config.FORCE_PASSWORD_CHANGE: UserOptions.objects.set_force_passwd_change(email) # update the user's optional info # update nikename if record[2]: try: nickname = record[2].strip() if len(nickname) <= 64 and '/' not in nickname: Profile.objects.add_or_update(email, nickname, '') except Exception as e: logger.error(e) # update role if record[3]: try: role = record[3].strip() if is_pro_version() and role in get_available_roles(): User.objects.update_role(email, role) except Exception as e: logger.error(e) # update quota if record[4]: try: space_quota_mb = int(record[4]) if space_quota_mb >= 0: space_quota = int(space_quota_mb) * get_file_size_unit( 'MB') seafile_api.set_user_quota(email, space_quota) except Exception as e: logger.error(e) send_html_email_with_dj_template( email, dj_template='sysadmin/user_batch_add_email.html', subject=_('You are invited to join %s') % get_site_name(), context={ 'user': email2nickname(request.user.username), 'email': email, 'password': password, }) user = User.objects.get(email=email) info = {} info['email'] = email info['name'] = email2nickname(email) info['contact_email'] = email2contact_email(email) info['is_staff'] = user.is_staff info['is_active'] = user.is_active info['quota_usage'] = seafile_api.get_user_self_usage(user.email) info['quota_total'] = seafile_api.get_user_quota(user.email) info['create_time'] = timestamp_to_isoformat_timestr(user.ctime) info['role'] = get_user_role(user) result['success'].append(info) # send admin operation log signal admin_op_detail = { "email": email, } admin_operation.send(sender=None, admin_name=request.user.username, operation=USER_ADD, detail=admin_op_detail) return Response(result)
def get(self, request): """ Return file info. """ # argument check doc_id = request.GET.get('doc_id', '') if not doc_id: error_msg = 'doc_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_info = cache.get('BISHENG_OFFICE_' + doc_id) if not file_info: error_msg = 'doc_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) username = file_info.get('username') if not username: error_msg = 'username invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) repo_id = file_info.get('repo_id') if not repo_id: error_msg = 'repo_id invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_path = file_info.get('file_path') if not file_path: error_msg = 'file_path invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) file_path = normalize_file_path(file_path) # resource check try: User.objects.get(email=username) except User.DoesNotExist: error_msg = 'User %s not found.' % username return api_error(status.HTTP_404_NOT_FOUND, error_msg) if not seafile_api.get_repo(repo_id): error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) if not seafile_api.get_file_id_by_path(repo_id, file_path): error_msg = 'File %s not found.' % file_path return api_error(status.HTTP_404_NOT_FOUND, error_msg) # permission check parent_dir = os.path.dirname(file_path) permission = seafile_api.check_permission_by_path(repo_id, parent_dir, username) if not permission: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # get file basic info file_name = os.path.basename(file_path.rstrip('/')) filetype, fileext = get_file_type_and_ext(file_name) # get file raw url file_id = seafile_api.get_file_id_by_path(repo_id, file_path) download_token = seafile_api.get_fileserver_access_token(repo_id, file_id, 'download', username, use_onetime=True) raw_url = gen_file_get_url(download_token, file_name) # get avatar url url, _, _ = api_avatar_url(username, int(72)) # prepare file permission privilege = copy.deepcopy(BISHENG_OFFICE_PRIVILEGE) can_edit = file_info.get('can_edit', False) if not can_edit: privilege.remove('FILE_WRITE') # prepare response file_info = { 'doc': { 'docId': doc_id, 'title': file_name, 'mime_type': BISHENG_OFFICE_MIME_TYPE[fileext], 'fetchUrl': raw_url, 'thumbnail': "", 'fromApi': True }, 'user': { 'uid': username, 'oid': username, 'nickName': email2nickname(username), 'avatar': request.build_absolute_uri(url), 'privilege': privilege }, } return Response(file_info)
def get(self, request, file_id, format=None): """ WOPI endpoint for check file info """ token = request.GET.get('access_token', None) request_user, repo_id, file_path = get_file_info_by_token(token) repo = seafile_api.get_repo(repo_id) obj_id = seafile_api.get_file_id_by_path(repo_id, file_path) try: file_size = seafile_api.get_file_size(repo.store_id, repo.version, obj_id) except SearpcError as e: logger.error(e) return HttpResponse(json.dumps({}), status=500, content_type=json_content_type) if file_size == -1: logger.error('File %s not found.' % file_path) return HttpResponse(json.dumps({}), status=401, content_type=json_content_type) result = {} # necessary result['BaseFileName'] = os.path.basename(file_path) result['Size'] = file_size result['UserId'] = request_user result['Version'] = obj_id try: if is_pro_version(): result['OwnerId'] = seafile_api.get_repo_owner(repo_id) or \ seafile_api.get_org_repo_owner(repo_id) else: result['OwnerId'] = seafile_api.get_repo_owner(repo_id) except Exception as e: logger.error(e) return HttpResponse(json.dumps({}), status=500, content_type=json_content_type) # optional result['UserFriendlyName'] = email2nickname(request_user) absolute_uri = request.build_absolute_uri('/') result['PostMessageOrigin'] = urlparse.urljoin(absolute_uri, SITE_ROOT).strip('/') result['HidePrintOption'] = False result['HideSaveOption'] = False result['HideExportOption'] = False result['EnableOwnerTermination'] = True result['SupportsLocks'] = True result['SupportsGetLock'] = True result['SupportsUpdate'] = True filename = os.path.basename(file_path) filetype, fileext = get_file_type_and_ext(filename) try: is_locked, locked_by_me = check_file_lock(repo_id, file_path, request_user) except Exception as e: logger.error(e) return HttpResponse(json.dumps({}), status=500, content_type=json_content_type) perm = seafile_api.check_permission_by_path(repo_id, file_path, request_user) if ENABLE_OFFICE_WEB_APP_EDIT and not repo.encrypted and \ perm == 'rw' and ((not is_locked) or (is_locked and locked_by_me)) and \ fileext in OFFICE_WEB_APP_EDIT_FILE_EXTENSION: result['UserCanWrite'] = True return HttpResponse(json.dumps(result), status=200, content_type=json_content_type)
def file_edit(request, repo_id): repo = get_repo(repo_id) if not repo: raise Http404 if request.method == "POST": return file_edit_submit(request, repo_id) if get_user_permission(request, repo_id) != "rw": return render_permission_error(request, _(u"Unable to edit file")) path = request.GET.get("p", "/") if path[-1] == "/": path = path[:-1] u_filename = os.path.basename(path) filename = urllib2.quote(u_filename.encode("utf-8")) head_id = repo.head_cmmt_id obj_id = get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u"The file does not exist.")) token = web_get_access_token(repo_id, obj_id, "view", request.user.username) # generate path and link zipped = gen_path_link(path, repo.name) filetype, fileext = get_file_type_and_ext(filename) op = None err = "" file_content = None encoding = None file_encoding_list = FILE_ENCODING_LIST if filetype == TEXT or filetype == MARKDOWN or filetype == SF: if repo.encrypted: repo.password_set = seafserv_rpc.is_passwd_set(repo_id, request.user.username) if not repo.password_set: op = "decrypt" if not op: raw_path = gen_file_get_url(token, filename) file_enc = request.GET.get("file_enc", "auto") if not file_enc in FILE_ENCODING_LIST: file_enc = "auto" err, file_content, encoding = repo_file_get(raw_path, file_enc) if encoding and encoding not in FILE_ENCODING_LIST: file_encoding_list.append(encoding) else: err = _(u"Edit online is not offered for this type of file.") return render_to_response( "file_edit.html", { "repo": repo, "u_filename": u_filename, "wiki_name": os.path.splitext(u_filename)[0], "path": path, "zipped": zipped, "filetype": filetype, "fileext": fileext, "op": op, "err": err, "file_content": file_content, "encoding": encoding, "file_encoding_list": file_encoding_list, "head_id": head_id, "from": request.GET.get("from", ""), "gid": request.GET.get("gid", ""), }, context_instance=RequestContext(request), )
def view_history_file_common(request, repo_id, ret_dict): username = request.user.username # check arguments repo = get_repo(repo_id) if not repo: raise Http404 path = request.GET.get('p', '/') commit_id = request.GET.get('commit_id', '') if not commit_id: raise Http404 obj_id = request.GET.get('obj_id', '') if not obj_id: raise Http404 # construct some varibles u_filename = os.path.basename(path) current_commit = get_commit(commit_id) if not current_commit: raise Http404 # Check whether user has permission to view file and get file raw path, # render error page if permission is deny. raw_path, user_perm = get_file_view_path_and_perm(request, repo_id, obj_id, u_filename) request.user_perm = user_perm # get file type and extension filetype, fileext = get_file_type_and_ext(u_filename) if user_perm: # Check file size fsize = get_file_size(obj_id) if fsize > FILE_PREVIEW_MAX_SIZE: err = _(u'File size surpasses %s, can not be opened online.') % \ filesizeformat(FILE_PREVIEW_MAX_SIZE) ret_dict['err'] = err elif filetype in (DOCUMENT, PDF) and HAS_OFFICE_CONVERTER and fsize > OFFICE_PREVIEW_MAX_SIZE: err = _(u'File size surpasses %s, can not be opened online.') % \ filesizeformat(OFFICE_PREVIEW_MAX_SIZE) ret_dict['err'] = err else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, raw_path, ret_dict) elif filetype == DOCUMENT: handle_document(raw_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(raw_path, obj_id, fileext, ret_dict) else: pass # populate return value dict ret_dict['repo'] = repo ret_dict['obj_id'] = obj_id ret_dict['file_name'] = u_filename ret_dict['path'] = path ret_dict['current_commit'] = current_commit ret_dict['fileext'] = fileext ret_dict['raw_path'] = raw_path if not ret_dict.has_key('filetype'): ret_dict['filetype'] = filetype ret_dict['use_pdfjs'] = USE_PDFJS if not repo.encrypted: ret_dict['search_repo_id'] = repo.id
def view_shared_dir(request, fileshare): token = fileshare.token password_check_passed, err_msg = check_share_link_common( request, fileshare) if not password_check_passed: d = { 'token': token, 'view_name': 'view_shared_dir', 'err_msg': err_msg } return render(request, 'share_access_validation.html', d) username = fileshare.username repo_id = fileshare.repo_id # Get path from frontend, use '/' if missing, and construct request path # with fileshare.path to real path, used to fetch dirents by RPC. req_path = request.GET.get('p', '/') if req_path[-1] != '/': req_path += '/' if req_path == '/': real_path = fileshare.path else: real_path = posixpath.join(fileshare.path, req_path.lstrip('/')) if real_path[-1] != '/': # Normalize dir path real_path += '/' repo = get_repo(repo_id) if not repo: raise Http404 if repo.encrypted or not \ seafile_api.check_permission_by_path(repo_id, '/', username): return render_error(request, _(u'Permission denied')) # Check path still exist, otherwise show error if not seafile_api.get_dir_id_by_path(repo.id, fileshare.path): return render_error(request, _('"%s" does not exist.') % fileshare.path) if fileshare.path == '/': # use repo name as dir name if share whole library dir_name = repo.name else: dir_name = os.path.basename(real_path[:-1]) current_commit = seaserv.get_commits(repo_id, 0, 1)[0] file_list, dir_list, dirent_more = get_repo_dirents( request, repo, current_commit, real_path) # generate dir navigator if fileshare.path == '/': zipped = gen_path_link(req_path, repo.name) else: zipped = gen_path_link(req_path, os.path.basename(fileshare.path[:-1])) if req_path == '/': # When user view the root of shared dir.. # increase shared link view_cnt, fileshare = FileShare.objects.get(token=token) fileshare.view_cnt = F('view_cnt') + 1 fileshare.save() traffic_over_limit = user_traffic_over_limit(fileshare.username) permissions = fileshare.get_permissions() # mode to view dir/file items mode = request.GET.get('mode', 'list') if mode != 'list': mode = 'grid' thumbnail_size = THUMBNAIL_DEFAULT_SIZE if mode == 'list' else THUMBNAIL_SIZE_FOR_GRID for f in file_list: file_type, file_ext = get_file_type_and_ext(f.obj_name) if file_type == IMAGE: f.is_img = True if file_type == VIDEO: f.is_video = True if (file_type == IMAGE or file_type == VIDEO) and ENABLE_THUMBNAIL: if os.path.exists( os.path.join(THUMBNAIL_ROOT, str(thumbnail_size), f.obj_id)): req_image_path = posixpath.join(req_path, f.obj_name) src = get_share_link_thumbnail_src(token, thumbnail_size, req_image_path) f.encoded_thumbnail_src = urlquote(src) return render( request, 'view_shared_dir.html', { 'repo': repo, 'token': token, 'path': req_path, 'username': username, 'dir_name': dir_name, 'file_list': file_list, 'dir_list': dir_list, 'zipped': zipped, 'traffic_over_limit': traffic_over_limit, 'permissions': permissions, 'ENABLE_THUMBNAIL': ENABLE_THUMBNAIL, 'mode': mode, 'thumbnail_size': thumbnail_size, })
def file_edit(request, repo_id): repo = get_repo(repo_id) if not repo: raise Http404 if request.method == 'POST': return file_edit_submit(request, repo_id) if get_user_permission(request, repo_id) != 'rw': return render_permission_error(request, _(u'Unable to edit file')) path = request.GET.get('p', '/') if path[-1] == '/': path = path[:-1] u_filename = os.path.basename(path) filename = urllib2.quote(u_filename.encode('utf-8')) head_id = repo.head_cmmt_id obj_id = get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u'The file does not exist.')) token = web_get_access_token(repo_id, obj_id, 'view', request.user.username) # generate path and link zipped = gen_path_link(path, repo.name) filetype, fileext = get_file_type_and_ext(filename) op = None err = '' file_content = None encoding = None file_encoding_list = FILE_ENCODING_LIST if filetype == TEXT or filetype == MARKDOWN or filetype == SF: if repo.encrypted: repo.password_set = seafserv_rpc.is_passwd_set(repo_id, request.user.username) if not repo.password_set: op = 'decrypt' if not op: raw_path = gen_file_get_url(token, filename) file_enc = request.GET.get('file_enc', 'auto') if not file_enc in FILE_ENCODING_LIST: file_enc = 'auto' err, file_content, encoding = repo_file_get(raw_path, file_enc) if encoding and encoding not in FILE_ENCODING_LIST: file_encoding_list.append(encoding) else: err = _(u'Edit online is not offered for this type of file.') # Redirect to different place according to from page when user click # cancel button on file edit page. cancel_url = reverse('repo_view_file', args=[repo.id]) + '?p=' + urlquote(path) page_from = request.GET.get('from', '') gid = request.GET.get('gid', '') wiki_name = os.path.splitext(u_filename)[0] if page_from == 'wiki_page_edit' or page_from == 'wiki_page_new': cancel_url = reverse('group_wiki', args=[gid, wiki_name]) elif page_from == 'personal_wiki_page_edit' or page_from == 'personal_wiki_page_new': cancel_url = reverse('personal_wiki', args=[wiki_name]) search_repo_id = None if not repo.encrypted: search_repo_id = repo.id return render_to_response('file_edit.html', { 'repo':repo, 'u_filename':u_filename, 'wiki_name': wiki_name, 'path':path, 'zipped':zipped, 'filetype':filetype, 'fileext':fileext, 'op':op, 'err':err, 'file_content':file_content, 'encoding': encoding, 'file_encoding_list':file_encoding_list, 'head_id': head_id, 'from': page_from, 'gid': gid, 'cancel_url': cancel_url, 'search_repo_id': search_repo_id, }, context_instance=RequestContext(request))
def get(self, request, token): """ Only used for get dirents in a folder share link. Permission checking: 1, If enable SHARE_LINK_LOGIN_REQUIRED, user must have been authenticated. 2, If enable ENABLE_SHARE_LINK_AUDIT, user must have been authenticated, or have been audited. 3, If share link is encrypted, share link password must have been checked. """ # argument check thumbnail_size = request.GET.get('thumbnail_size', 48) try: thumbnail_size = int(thumbnail_size) except ValueError: error_msg = 'thumbnail_size invalid.' return api_error(status.HTTP_400_BAD_REQUEST, error_msg) # permission check # check if login required if SHARE_LINK_LOGIN_REQUIRED and \ not request.user.is_authenticated: error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # check share link audit if is_pro_version() and ENABLE_SHARE_LINK_AUDIT and \ not request.user.is_authenticated and \ not request.session.get('anonymous_email'): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) # resource check try: share_link = FileShare.objects.get(token=token) except FileShare.DoesNotExist: error_msg = 'Share link %s not found.' % token return api_error(status.HTTP_404_NOT_FOUND, error_msg) # check share link password if share_link.is_encrypted() and not check_share_link_access( request, token): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) if share_link.s_type != 'd': error_msg = 'Share link %s is not a folder share link.' % token return api_error(status.HTTP_400_BAD_REQUEST, error_msg) repo_id = share_link.repo_id repo = seafile_api.get_repo(repo_id) if not repo: error_msg = 'Library %s not found.' % repo_id return api_error(status.HTTP_404_NOT_FOUND, error_msg) share_link_path = share_link.path request_path = request.GET.get('path', '/') if request_path == '/': path = share_link_path else: path = posixpath.join(share_link_path, request_path.strip('/')) path = normalize_dir_path(path) dir_id = seafile_api.get_dir_id_by_path(repo_id, path) if not dir_id: error_msg = 'Folder %s not found.' % request_path return api_error(status.HTTP_404_NOT_FOUND, error_msg) try: current_commit = seafile_api.get_commit_list(repo_id, 0, 1)[0] dirent_list = seafile_api.list_dir_by_commit_and_path( repo_id, current_commit.id, path, -1, -1) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) result = [] for dirent in dirent_list: # don't return parent folder(share link path) info to user # so use request_path here dirent_path = posixpath.join(request_path, dirent.obj_name) dirent_info = {} dirent_info['size'] = dirent.size dirent_info['last_modified'] = timestamp_to_isoformat_timestr( dirent.mtime) if stat.S_ISDIR(dirent.mode): dirent_info['is_dir'] = True dirent_info['folder_path'] = normalize_dir_path(dirent_path) dirent_info['folder_name'] = dirent.obj_name else: dirent_info['is_dir'] = False dirent_info['file_path'] = normalize_file_path(dirent_path) dirent_info['file_name'] = dirent.obj_name file_type, file_ext = get_file_type_and_ext(dirent.obj_name) if file_type in (IMAGE, XMIND) or \ (file_type == VIDEO and ENABLE_VIDEO_THUMBNAIL): if os.path.exists( os.path.join(THUMBNAIL_ROOT, str(thumbnail_size), dirent.obj_id)): req_image_path = posixpath.join( request_path, dirent.obj_name) src = get_share_link_thumbnail_src( token, thumbnail_size, req_image_path) dirent_info['encoded_thumbnail_src'] = urlquote(src) result.append(dirent_info) return Response({'dirent_list': result})
def group_discuss(request, group): if group.is_pub: raise Http404 username = request.user.username form = MessageForm() # remove user notifications UserNotification.objects.seen_group_msg_notices(username, group.id) # Get all group members. members = get_group_members(group.id) """group messages""" # Show 15 group messages per page. paginator = Paginator(GroupMessage.objects.filter(group_id=group.id).order_by("-timestamp"), 15) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get("page", "1")) except ValueError: page = 1 # If page request (9999) is out of range, deliver last page of results. try: group_msgs = paginator.page(page) except (EmptyPage, InvalidPage): group_msgs = paginator.page(paginator.num_pages) group_msgs.page_range = paginator.get_page_range(group_msgs.number) # Force evaluate queryset to fix some database error for mysql. group_msgs.object_list = list(group_msgs.object_list) msg_attachments = MessageAttachment.objects.filter(group_message__in=group_msgs.object_list) msg_replies = MessageReply.objects.filter(reply_to__in=group_msgs.object_list) reply_to_list = [r.reply_to_id for r in msg_replies] for msg in group_msgs.object_list: msg.reply_cnt = reply_to_list.count(msg.id) msg.replies = [] for r in msg_replies: if msg.id == r.reply_to_id: msg.replies.append(r) msg.replies = msg.replies[-3:] msg.attachments = [] for att in msg_attachments: if att.group_message_id != msg.id: continue # Attachment name is file name or directory name. # If is top directory, use repo name instead. path = att.path if path == "/": repo = get_repo(att.repo_id) if not repo: # TODO: what should we do here, tell user the repo # is no longer exists? continue att.name = repo.name else: path = path.rstrip("/") # cut out last '/' if possible att.name = os.path.basename(path) # Load to discuss page if attachment is a image and from recommend. if att.attach_type == "file" and att.src == "recommend": att.filetype, att.fileext = get_file_type_and_ext(att.name) if att.filetype == IMAGE: att.obj_id = get_file_id_by_path(att.repo_id, path) if not att.obj_id: att.err = _(u"File does not exist") else: att.token = seafile_api.get_fileserver_access_token(att.repo_id, att.obj_id, "view", username) att.img_url = gen_file_get_url(att.token, att.name) msg.attachments.append(att) # get available modules(wiki, etc) mods_available = get_available_mods_by_group(group.id) mods_enabled = get_enabled_mods_by_group(group.id) return render_to_response( "group/group_discuss.html", { "group": group, "is_staff": group.is_staff, "group_msgs": group_msgs, "form": form, "mods_enabled": mods_enabled, "mods_available": mods_available, }, context_instance=RequestContext(request), )
def view_priv_shared_file(request, token): """View private shared file. """ try: pfs = PrivateFileDirShare.objects.get_priv_file_dir_share_by_token(token) except PrivateFileDirShare.DoesNotExist: raise Http404 repo_id = pfs.repo_id repo = get_repo(repo_id) if not repo: raise Http404 username = request.user.username if username != pfs.from_user and username != pfs.to_user: raise Http404 # permission check path = normalize_file_path(pfs.path) obj_id = seafile_api.get_file_id_by_path(repo.id, path) if not obj_id: raise Http404 filename = os.path.basename(path) filetype, fileext = get_file_type_and_ext(filename) access_token = seafile_api.get_fileserver_access_token(repo.id, obj_id, 'view', username) raw_path = gen_file_get_url(access_token, filename) inner_path = gen_inner_file_get_url(access_token, filename) # get file content ret_dict = {'err': '', 'file_content': '', 'encoding': '', 'file_enc': '', 'file_encoding_list': [], 'html_exists': False, 'filetype': filetype} fsize = get_file_size(repo.store_id, repo.version, obj_id) exceeds_limit, err_msg = file_size_exceeds_preview_limit(fsize, filetype) if exceeds_limit: ret_dict['err'] = err_msg else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, inner_path, ret_dict) elif filetype == DOCUMENT: handle_document(inner_path, obj_id, fileext, ret_dict) elif filetype == SPREADSHEET: handle_spreadsheet(inner_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(inner_path, obj_id, fileext, ret_dict) accessible_repos = get_unencry_rw_repos_by_user(request) save_to_link = reverse('save_private_file_share', args=[pfs.token]) return render_to_response('shared_file_view.html', { 'repo': repo, 'obj_id': obj_id, 'path': path, 'file_name': filename, 'file_size': fsize, 'access_token': access_token, 'fileext': fileext, 'raw_path': raw_path, 'shared_by': pfs.from_user, 'err': ret_dict['err'], 'file_content': ret_dict['file_content'], 'encoding': ret_dict['encoding'], 'file_encoding_list':ret_dict['file_encoding_list'], 'html_exists': ret_dict['html_exists'], 'html_detail': ret_dict.get('html_detail', {}), 'filetype': ret_dict['filetype'], 'use_pdfjs':USE_PDFJS, 'accessible_repos': accessible_repos, 'save_to_link': save_to_link, }, context_instance=RequestContext(request))
def slug(request, slug, file_path="home.md"): """Show wiki page. """ # get wiki object or 404 wiki = get_object_or_404(Wiki, slug=slug) file_path = "/" + file_path is_dir = None file_id = seafile_api.get_file_id_by_path(wiki.repo_id, file_path) if file_id: is_dir = False dir_id = seafile_api.get_dir_id_by_path(wiki.repo_id, file_path) if dir_id: is_dir = True # compatible with old wiki url if is_dir is None: if len(file_path.split('.')) == 1: new_path = file_path[1:] + '.md' return HttpResponseRedirect(reverse('wiki:slug', args=[slug, new_path])) # perm check req_user = request.user.username if not req_user and not wiki.has_read_perm(request): return redirect('auth_login') else: if not wiki.has_read_perm(request): return render_permission_error(request, _('Unable to view Wiki')) file_type, ext = get_file_type_and_ext(posixpath.basename(file_path)) if file_type == IMAGE: file_url = reverse('view_lib_file', args=[wiki.repo_id, file_path]) return HttpResponseRedirect(file_url + "?raw=1") if not req_user: user_can_write = False elif req_user == wiki.username or check_folder_permission( request, wiki.repo_id, '/') == 'rw': user_can_write = True else: user_can_write = False is_public_wiki = False if wiki.permission == 'public': is_public_wiki = True has_index = False dirs = seafile_api.list_dir_by_path(wiki.repo_id, '/') for dir_obj in dirs: if dir_obj.obj_name == 'index.md': has_index = True break try: fs = FileShare.objects.get(repo_id=wiki.repo_id, path='/') except FileShare.DoesNotExist: fs = FileShare.objects.create_dir_link(wiki.username, wiki.repo_id, '/', permission='view_download') wiki.permission = 'public' wiki.save() is_public_wiki = True return render(request, "wiki/wiki.html", { "wiki": wiki, "page_name": file_path, "shared_token": fs.token, "shared_type": fs.s_type, "user_can_write": user_can_write, "file_path": file_path, "repo_id": wiki.repo_id, "search_repo_id": wiki.repo_id, "search_wiki": True, "is_public_wiki": is_public_wiki, "is_dir": is_dir, "has_index": has_index, })
def view_file(request, repo_id): """ Steps to view file: 1. Get repo id and file path. 2. Check user's permission. 3. Check whether this file can be viewed online. 4.1 Get file content if file is text file. 4.2 Prepare flash if file is document. 4.3 Prepare or use pdfjs if file is pdf. 4.4 Other file return it's raw path. """ username = request.user.username # check arguments repo = get_repo(repo_id) if not repo: raise Http404 path = request.GET.get("p", "/") obj_id = get_file_id_by_path(repo_id, path) if not obj_id: return render_error(request, _(u"File does not exist")) # construct some varibles u_filename = os.path.basename(path) filename_utf8 = urllib2.quote(u_filename.encode("utf-8")) current_commit = get_commits(repo_id, 0, 1)[0] # Check whether user has permission to view file and get file raw path, # render error page if permission is deny. raw_path, user_perm = get_file_view_path_and_perm(request, repo_id, obj_id, u_filename) if not user_perm: return render_permission_error(request, _(u"Unable to view file")) # get file type and extension filetype, fileext = get_file_type_and_ext(u_filename) img_prev = None img_next = None ret_dict = { "err": "", "file_content": "", "encoding": "", "file_enc": "", "file_encoding_list": [], "swf_exists": False, "filetype": filetype, } # Check file size fsize = get_file_size(obj_id) if fsize > FILE_PREVIEW_MAX_SIZE: from django.template.defaultfilters import filesizeformat err = _(u"File size surpasses %s, can not be opened online.") % filesizeformat(FILE_PREVIEW_MAX_SIZE) ret_dict["err"] = err else: """Choose different approach when dealing with different type of file.""" if is_textual_file(file_type=filetype): handle_textual_file(request, filetype, raw_path, ret_dict) if filetype == MARKDOWN: c = ret_dict["file_content"] ret_dict["file_content"] = convert_md_link(c, repo_id, username) elif filetype == DOCUMENT: handle_document(raw_path, obj_id, fileext, ret_dict) elif filetype == PDF: handle_pdf(raw_path, obj_id, fileext, ret_dict) elif filetype == IMAGE: parent_dir = os.path.dirname(path) dirs = list_dir_by_path(current_commit.id, parent_dir) if not dirs: raise Http404 img_list = [] for dirent in dirs: if not stat.S_ISDIR(dirent.props.mode): fltype, flext = get_file_type_and_ext(dirent.obj_name) if fltype == "Image": img_list.append(dirent.obj_name) if len(img_list) > 1: img_list.sort(lambda x, y: cmp(x.lower(), y.lower())) cur_img_index = img_list.index(u_filename) if cur_img_index != 0: img_prev = os.path.join(parent_dir, img_list[cur_img_index - 1]) if cur_img_index != len(img_list) - 1: img_next = os.path.join(parent_dir, img_list[cur_img_index + 1]) else: pass # generate file path navigator zipped = gen_path_link(path, repo.name) # file shared link l = FileShare.objects.filter(repo_id=repo_id).filter(username=username).filter(path=path) fileshare = l[0] if len(l) > 0 else None http_or_https = request.is_secure() and "https" or "http" domain = RequestSite(request).domain if fileshare: file_shared_link = gen_shared_link(request, fileshare.token, "f") else: file_shared_link = "" # my contacts used in shared link autocomplete contacts = Contact.objects.filter(user_email=username) """List repo groups""" # Get groups this repo is shared. if request.user.org: org_id = request.user.org["org_id"] repo_shared_groups = get_org_groups_by_repo(org_id, repo_id) else: repo_shared_groups = get_shared_groups_by_repo(repo_id) # Filter out groups that user in joined. groups = [x for x in repo_shared_groups if is_group_user(x.id, username)] if len(groups) > 1: ctx = {} ctx["groups"] = groups repogrp_str = render_to_string("snippets/repo_group_list.html", ctx) else: repogrp_str = "" file_path_hash = md5_constructor(urllib2.quote(path.encode("utf-8"))).hexdigest()[:12] # fetch file contributors and latest contributor contributors, last_modified, last_commit_id = get_file_contributors( repo_id, path.encode("utf-8"), file_path_hash, obj_id ) latest_contributor = contributors[0] if contributors else None # check whether file is starred is_starred = False org_id = -1 if request.user.org: org_id = request.user.org["org_id"] is_starred = is_file_starred(username, repo.id, path.encode("utf-8"), org_id) template = "view_file_%s.html" % ret_dict["filetype"].lower() return render_to_response( template, { "repo": repo, "obj_id": obj_id, "filename": u_filename, "path": path, "zipped": zipped, "current_commit": current_commit, "fileext": fileext, "raw_path": raw_path, "fileshare": fileshare, "protocol": http_or_https, "domain": domain, "file_shared_link": file_shared_link, "contacts": contacts, "err": ret_dict["err"], "file_content": ret_dict["file_content"], "file_enc": ret_dict["file_enc"], "encoding": ret_dict["encoding"], "file_encoding_list": ret_dict["file_encoding_list"], "swf_exists": ret_dict["swf_exists"], "filetype": ret_dict["filetype"], "applet_root": get_ccnetapplet_root(), "groups": groups, "DOCUMENT_CONVERTOR_ROOT": DOCUMENT_CONVERTOR_ROOT, "use_pdfjs": USE_PDFJS, "contributors": contributors, "latest_contributor": latest_contributor, "last_modified": last_modified, "last_commit_id": last_commit_id, "repo_group_str": repogrp_str, "is_starred": is_starred, "user_perm": user_perm, "img_prev": img_prev, "img_next": img_next, }, context_instance=RequestContext(request), )