Пример #1
0
def FileUpdateEventHandler(session, msg):
    elements = msg.body.split('\t')
    if len(elements) != 3:
        logging.warning("got bad message: %s", elements)
        return

    repo_id = elements[1]
    commit_id = elements[2]

    org_id = get_org_id_by_repo_id(repo_id)

    commit = get_commit(repo_id, 1, commit_id)
    if commit is None:
        commit = get_commit(repo_id, 0, commit_id)
        if commit is None:
            return

    time = datetime.datetime.utcfromtimestamp(msg.ctime)

    #KEEPER:
    #logging.info("FILE UPDATE EVENT: %s, start generate_certificate", commit.desc)
    generate_certificate_by_commit(commit) 

    save_file_update_event(session, time, commit.creator_name, org_id, \
                           repo_id, commit_id, commit.desc)
Пример #2
0
def FileUpdateEventHandler(session, msg):
    elements = msg['content'].split('\t')
    if len(elements) != 3:
        logging.warning("got bad message: %s", elements)
        return

    repo_id = elements[1]
    commit_id = elements[2]

    org_id = get_org_id_by_repo_id(repo_id)

    commit = get_commit(repo_id, 1, commit_id)
    if commit is None:
        commit = get_commit(repo_id, 0, commit_id)
        if commit is None:
            return

    time = datetime.datetime.utcfromtimestamp(msg['ctime'])

    # KEEPER
    logging.info("FILE UPDATE EVENT: %s, try generate_certificate", commit.desc)
    generate_certificate_by_commit(commit)

    save_file_update_event(session, time, commit.creator_name, org_id,
                           repo_id, commit_id, commit.desc)
Пример #3
0
 def to_dict(self):
     repo = seafile_api.get_repo(self.repo_id)
     if not repo:
         return None
     commit = seaserv.get_commit(repo.id, repo.revision, self.revision_id)
     email = commit.creator_name
     return {
         "tag": self.tag.name,
         "tag_creator": self.username,
         "revision": {
             "repo_id":
             self.repo_id,
             "commit_id":
             self.revision_id,
             "email":
             email,
             "name":
             email2nickname(email),
             "contact_email":
             email2contact_email(email),
             "time":
             timestamp_to_isoformat_timestr(commit.ctime),
             "description":
             commit.desc,
             "link":
             reverse("repo_history_view", args=[self.repo_id]) +
             "?commit_id=%s" % self.revision_id
         }
     }
Пример #4
0
 def get_current_commit(self):
     commit_id = self.request.GET.get('commit_id', '')
     if not commit_id:
         return HttpResponseRedirect(reverse('repo', args=[self.repo.id]))
     current_commit = get_commit(commit_id)
     if not current_commit:
         current_commit = get_commits(self.repo.id, 0, 1)[0]
     return current_commit
Пример #5
0
 def get_current_commit(self):
     commit_id = self.request.GET.get('commit_id', '')
     if not commit_id:
         return HttpResponseRedirect(reverse('repo', args=[self.repo.id]))
     current_commit = get_commit(commit_id)
     if not current_commit:
         current_commit = get_commits(self.repo.id, 0, 1)[0]
     return current_commit
Пример #6
0
def test_repo(repo_id):
    repo = seafile_api.get_repo(repo_id)
    print vars(repo)
    current_commit = seaserv.get_commit(repo.id, repo.version, repo.head_cmmt_id)
    print vars(current_commit)
    path = '/'
    file_list, dir_list = get_repo_dirents(repo, current_commit, path)
    for f in file_list:
        print vars(f)
Пример #7
0
def get_commit_before_new_merge(commit):
    """Traverse parents of ``commit``, and get a commit which is not a new merge.

    Pre-condition: ``commit`` must be a new merge and not conflict.

    Arguments:
    - `commit`:
    """
    assert new_merge_with_no_conflict(commit) is True

    while(new_merge_with_no_conflict(commit)):
        p1 = seaserv.get_commit(commit.repo_id, commit.version, commit.parent_id)
        p2 = seaserv.get_commit(commit.repo_id, commit.version, commit.second_parent_id)
        commit = p1 if p1.ctime > p2.ctime else p2

    assert new_merge_with_no_conflict(commit) is False

    return commit
Пример #8
0
def get_commit_before_new_merge(commit):
    """Traverse parents of ``commit``, and get a commit which is not a new merge.

    Pre-condition: ``commit`` must be a new merge and not conflict.

    Arguments:
    - `commit`:
    """
    assert new_merge_with_no_conflict(commit) is True

    while(new_merge_with_no_conflict(commit)):
        p1 = seaserv.get_commit(commit.repo_id, commit.version, commit.parent_id)
        p2 = seaserv.get_commit(commit.repo_id, commit.version, commit.second_parent_id)
        commit = p1 if p1.ctime > p2.ctime else p2

    assert new_merge_with_no_conflict(commit) is False

    return commit
def get_repo_last_modify(repo):
    """ Get last modification time for a repo.

    If head commit id of a repo is provided, we use that commit as last commit,
    otherwise falls back to getting last commit of a repo which is time
    consuming.
    """
    last_cmmt = None
    if repo.head_cmmt_id is not None:
        last_cmmt = seaserv.get_commit(repo.id, repo.version, repo.head_cmmt_id)
    return last_cmmt.ctime if last_cmmt else 0
Пример #10
0
def get_repo_last_modify(repo):
    """ Get last modification time for a repo.

    If head commit id of a repo is provided, we use that commit as last commit,
    otherwise falls back to getting last commit of a repo which is time
    consuming.
    """
    last_cmmt = None
    if repo.head_cmmt_id is not None:
        last_cmmt = seaserv.get_commit(repo.id, repo.version,
                                       repo.head_cmmt_id)
    return last_cmmt.ctime if last_cmmt else 0
Пример #11
0
def FileUpdateEventHandler(session, msg):
    elements = msg.body.split('\t')
    if len(elements) != 3:
        logging.warning("got bad message: %s", elements)
        return

    repo_id = elements[1]
    commit_id = elements[2]

    org_id = get_org_id_by_repo_id(repo_id)

    commit = get_commit(repo_id, 1, commit_id)
    if commit is None:
        commit = get_commit(repo_id, 0, commit_id)
        if commit is None:
            return

    time = datetime.datetime.utcfromtimestamp(msg.ctime)

    save_file_update_event(session, time, commit.creator_name, org_id, repo_id,
                           commit_id, commit.desc)
Пример #12
0
def get_repo_last_modify(repo):
    """ Get last modification time for a repo.

    If head commit id of a repo is provided, we use that commit as last commit,
    otherwise falls back to getting last commit of a repo which is time
    consuming.
    """
    if repo.head_cmmt_id is not None:
        last_cmmt = seaserv.get_commit(repo.id, repo.version, repo.head_cmmt_id)
    else:
        logger = logging.getLogger(__name__)
        logger.info('[repo %s] head_cmmt_id is missing.' % repo.id)
        last_cmmt = seafile_api.get_commit_list(repo.id, 0, 1)[0]
    return last_cmmt.ctime if last_cmmt else 0
Пример #13
0
def get_repo_last_modify(repo):
    """ Get last modification time for a repo.

    If head commit id of a repo is provided, we use that commit as last commit,
    otherwise falls back to getting last commit of a repo which is time
    consuming.
    """
    if repo.head_cmmt_id is not None:
        last_cmmt = seaserv.get_commit(repo.id, repo.version, repo.head_cmmt_id)
    else:
        logger = logging.getLogger(__name__)
        logger.info('[repo %s] head_cmmt_id is missing.' % repo.id)
        last_cmmt = seafile_api.get_commit_list(repo.id, 0, 1)[0]
    return last_cmmt.ctime if last_cmmt else 0
Пример #14
0
 def to_dict(self):
     repo = seafile_api.get_repo(self.repo_id)
     if not repo:
         return None
     commit = seaserv.get_commit(repo.id, repo.revision, self.revision_id)
     email = commit.creator_name
     return  {"tag":self.tag.name,
              "tag_creator": self.username,
              "revision": {
                  "repo_id": self.repo_id,
                  "commit_id": self.revision_id,
                  "email": email,
                  "name": email2nickname(email),
                  "contact_email": email2contact_email(email),
                  "time": timestamp_to_isoformat_timestr(commit.ctime),
                  "description": commit.desc,
                  "link": reverse("repo_history_view", args=[self.repo_id])+"?commit_id=%s"%self.revision_id
                  }}
Пример #15
0
    def _decorated(view, request, *args, **kwargs):
        if request.method in ["POST", "PUT"]:
            repo_id = request.data.get('repo_id', '')
            commit_id = request.data.get('commit_id', '')
            tag_names = request.data.get('tag_names', None)
        if not repo_id:
            error_msg = "Repo can not be empty"
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            error_msg = "Library %s not found" % repo_id
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        if not commit_id:
            commit_id = repo.head_cmmt_id
        commit = seaserv.get_commit(repo.id, repo.version, commit_id)
        if not commit:
            error_msg = "Commit %s not found" % commit_id
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        if tag_names is None:
            error_msg = "Tag can not be empty"
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
        names = []
        if not tag_names.strip():
            if request.method == "POST":
                error_msg = "Tag can not be empty"
                return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
        else:
            names = [name.strip() for name in tag_names.split(',')]
            for name in names:
                if not check_tagname(name):
                    error_msg = _(
                        "Tag can only contains letters, numbers, dot, hyphen or underscore"
                    )
                    return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        if check_folder_permission(request, repo_id, '/') != 'rw':
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        return func(view, request, repo_id, commit_id, names, *args, **kwargs)
Пример #16
0
    def _get_events_inner(ev_session, username, start, limit, org_id=None):
        '''Read events from seafevents database, and remove events that are
        no longer valid

        Return 'limit' events or less than 'limit' events if no more events remain
        '''
        valid_events = []
        next_start = start
        while True:
            if org_id > 0:
                events = seafevents.get_org_user_events(ev_session, org_id,
                                                        username, next_start,
                                                        limit)
            else:
                events = seafevents.get_user_events(ev_session, username,
                                                    next_start, limit)
            if not events:
                break

            for ev in events:
                if ev.etype == 'repo-update':
                    repo = seafile_api.get_repo(ev.repo_id)
                    if not repo:
                        # delete the update event for repo which has been deleted
                        seafevents.delete_event(ev_session, ev.uuid)
                        continue
                    if repo.encrypted:
                        repo.password_set = seafile_api.is_password_set(
                            repo.id, username)
                    ev.repo = repo
                    ev.commit = seaserv.get_commit(repo.id, repo.version, ev.commit_id)

                valid_events.append(ev)
                if len(valid_events) == limit:
                    break

            if len(valid_events) == limit:
                break
            next_start = next_start + len(valid_events)

        return valid_events
Пример #17
0
    def _get_events_inner(ev_session, username, start, limit, org_id=None):
        '''Read events from seafevents database, and remove events that are
        no longer valid

        Return 'limit' events or less than 'limit' events if no more events remain
        '''
        valid_events = []
        next_start = start
        while True:
            if org_id > 0:
                events = seafevents.get_org_user_events(
                    ev_session, org_id, username, next_start, limit)
            else:
                events = seafevents.get_user_events(ev_session, username,
                                                    next_start, limit)
            if not events:
                break

            for ev in events:
                if ev.etype == 'repo-update':
                    repo = seafile_api.get_repo(ev.repo_id)
                    if not repo:
                        # delete the update event for repo which has been deleted
                        seafevents.delete_event(ev_session, ev.uuid)
                        continue
                    if repo.encrypted:
                        repo.password_set = seafile_api.is_password_set(
                            repo.id, username)
                    ev.repo = repo
                    ev.commit = seaserv.get_commit(repo.id, repo.version,
                                                   ev.commit_id)

                valid_events.append(ev)
                if len(valid_events) == limit:
                    break

            if len(valid_events) == limit:
                break
            next_start = next_start + len(valid_events)

        return valid_events
Пример #18
0
    def _decorated(view, request, *args, **kwargs):
        if request.method in ["POST", "PUT"]:
            repo_id = request.data.get('repo_id', '')
            commit_id =request.data.get('commit_id', '')
            tag_names = request.data.get('tag_names', None)
        if not repo_id:
            error_msg = "Repo can not be empty"
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
        repo = seafile_api.get_repo(repo_id)
        if not repo:
            error_msg = "Library %s not found" % repo_id
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        if not commit_id:
            commit_id = repo.head_cmmt_id
        commit = seaserv.get_commit(repo.id, repo.version, commit_id)
        if not commit:
            error_msg = "Commit %s not found" % commit_id
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        if tag_names is None:
            error_msg = "Tag can not be empty"
            return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
        names = []
        if not tag_names.strip():
            if request.method == "POST":
                error_msg = "Tag can not be empty"
                return api_error(status.HTTP_400_BAD_REQUEST, error_msg)
        else:
            names = [name.strip() for name in tag_names.split(',')]
            for name in names:
                if not check_tagname(name):
                    error_msg = _("Tag can only contain letters, numbers, dot, hyphen or underscore.")
                    return api_error(status.HTTP_400_BAD_REQUEST, error_msg)

        if check_folder_permission(request, repo_id, '/') != 'rw':
            error_msg = 'Permission denied.'
            return api_error(status.HTTP_403_FORBIDDEN, error_msg)

        return func(view, request, repo_id, commit_id, names, *args, **kwargs)
Пример #19
0
def get_commit(repo_id, repo_version, commit_id):
    return seaserv.get_commit(repo_id, repo_version, commit_id)
Пример #20
0
def get_commit(repo_id, repo_version, commit_id):
    return seaserv.get_commit(repo_id, repo_version, commit_id)
Пример #21
0
def view_history_file_common(request, repo_id, ret_dict):
    # 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(repo.id, repo.version, 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  deny.
    raw_path, inner_path, user_perm = get_file_view_path_and_perm(
        request, repo_id, obj_id, path)
    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(repo.store_id, repo.version, 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, 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 fsize == 0:
                    ret_dict['err'] = _(u'Invalid file format.')
            elif filetype == PDF:
                handle_pdf(inner_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
Пример #22
0
def get_commit(commit_id):
    return seaserv.get_commit(commit_id)
Пример #23
0
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
Пример #24
0
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:
            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 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
Пример #25
0
def get_commit(commit_id):
    return seaserv.get_commit(commit_id)
Пример #26
0
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
Пример #27
0
def recover_data():

    # open DB session
    session = SeafEventsSession()
    # QUERY CONDITIONS
    # Searc in FileUpdate Table
    # timestamp between '2018-02-02 05:05' and '2018-02-02 16:00'
    # q = session.query(FileUpdate).filter(FileUpdate.timestamp.between('2018-02-02 05:05','2018-02-02 16:00'))
    q = session.query(FileUpdate).filter(
        FileUpdate.timestamp.between('2018-03-31 05:05:08',
                                     '2018-03-31 05:05:42'))
    # order by creation, desc
    q = q.order_by(desc(FileUpdate.eid))
    events = q.all()

    # Generate common data structure as dict, will be used later
    # by reports, tasks ,etc.
    users = defaultdict(list)
    if events:
        for ev in events:
            ev.repo = get_repo(ev.repo_id)
            ev.local_time = utc_to_local(ev.timestamp)
            ev.time = int(ev.local_time.strftime('%s'))
            changes = get_diff(ev.repo.repo_id, '', ev.commit_id)
            c = get_commit(ev.repo.repo_id, ev.repo.version, ev.commit_id)
            # number of changes in event
            c_num = 0
            for k in changes:
                c_num += len(changes[k])

            if c.parent_id is None:
                # A commit is a first commit only if it's parent id is None.
                changes['cmt_desc'] = repo.desc
            elif c.second_parent_id is None:
                # Normal commit only has one parent.
                if c.desc.startswith('Changed library'):
                    changes['cmt_desc'] = 'Changed library name or description'
            else:
                # A commit is a merge only if it has two parents.
                changes['cmt_desc'] = 'No conflict in the merge.'

            changes['date_time'] = str(c.ctime)

            # ev.repo is saved in the dict to make seafobj manipulation possible
            users[ev.user].append({
                'event': {
                    'repo': ev.repo,
                    'time': str(ev.local_time),
                    'library': ev.repo.name,
                    'encrypted': ev.repo.encrypted,
                    'file_oper': ev.file_oper
                },
                'details': changes,
                'changes_num': c_num
            })

    changed_files = defaultdict(dict)
    # for the moment we save modified and new files in separate dirs
    change = 'modified'
    # change = 'new'

    for key, events in users.iteritems():

        for e in events:
            if change in e['details']:
                ev = e['event']
                # encrypted libs will not be recovered
                if not ev['encrypted']:
                    lib = ev['library']
                    if not lib in changed_files[key]:
                        changed_files[key].update({
                            lib: {
                                'repo': ev['repo'],
                                'files': set(e['details'][change])
                            }
                        })
                    else:
                        changed_files[key][lib]['files'].update(
                            e['details'][change])

        # convert sets to lists to be serialized in json
        for u in changed_files:
            for l in changed_files[u]:
                changed_files[u][l]['files'] = list(
                    changed_files[u][l]['files'])

    # path in filesystem where the recovered files will be stored
    STORAGE_PATH = '/keeper/tmp/recovery/' + change + '/'
    # gnerate packages of changed files
    for u in changed_files:
        path = STORAGE_PATH + u
        for lib in changed_files[u]:
            dir = get_root_dir(changed_files[u][lib]['repo'])
            dest_path = path + "/" + lib
            for fn in changed_files[u][lib]['files']:
                copy_file(dir, fn, dest_path)
Пример #28
0
def generate_catalog_entry(repo):
    """
    Generate catalog entry in for the repo DB
    """
    reconnect_db()

    proj = {}

    try:
        proj["id"] = repo.id
        proj["name"] = repo.name
        email = get_repo_owner(repo.id)
        proj["owner"] = email
        user_name = get_user_name(email)
        if user_name != email:
            proj["owner_name"] = user_name
        proj["in_progress"] = True
        proj["modified"] = repo.last_modify

        commits = get_commits(repo.id, 0, 1)
        commit = get_commit(repo.id, repo.version, commits[0].id)
        dir = fs_mgr.load_seafdir(repo.id, repo.version, commit.root_id)
        file = dir.lookup(ARCHIVE_METADATA_TARGET)
        if file:
            md = file.get_content().decode('utf-8')
            md = parse_markdown(md)
            if md:
                # Author
                a = md.get("Author")
                if a:
                    a_list = a.split('\n')
                    authors = []
                    for _ in a_list:
                        author = {}
                        aa = _.split(';')
                        author['name'] = aa[0]
                        if len(aa) > 1 and aa[1].strip():
                            author['affs'] = [
                                x.strip() for x in aa[1].split('|')
                            ]
                            author['affs'] = [x for x in author['affs'] if x]
                        authors.append(author)
                    if a:
                        proj["authors"] = authors

                # Description
                d = md.get("Description")
                if d:
                    proj["description"] = d

                # Comments
                c = md.get("Comments")
                if c:
                    proj["comments"] = c

                # Title
                t = md.get("Title")
                if t:
                    proj["title"] = t
                    del proj["in_progress"]

                # Year
                y = md.get("Year")
                if y:
                    proj["year"] = y

                # Institute
                i = md.get("Institute")
                if i:
                    proj["institute"] = i

                proj["is_certified"] = is_certified_by_repo_id(repo.id)

        # add or update project metadata in DB
        c = Catalog.objects.add_or_update_by_repo_id(repo.id, email, proj,
                                                     repo.name)
        # Catalog_id
        proj["catalog_id"] = str(c.catalog_id)

    except Exception:
        msg = "repo_name: %s, id: %s" % (repo.name, repo.id)
        logging.error(msg)
        logging.error(traceback.format_exc())

    return proj
Пример #29
0
def view_history_file_common(request, repo_id, ret_dict):
    # 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(repo.id, repo.version, 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  deny.
    raw_path, inner_path, user_perm = get_file_view_path_and_perm(request, repo_id, obj_id, path)
    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(repo.store_id, repo.version, 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, 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 fsize == 0:
                    ret_dict["err"] = _(u"Invalid file format.")
            elif filetype == PDF:
                handle_pdf(inner_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
Пример #30
0
def get_catalog():

    catalog = []

    repos_all = seafile_api.get_repo_list(0, MAX_INT)
    #repos_all = [seafile_api.get_repo('a6d4ae75-b063-40bf-a3d9-dde74623bb2c')]

    for repo in repos_all:

        try:
            proj = {}
            proj["id"] = repo.id
            proj["name"] = repo.name
            email = get_repo_owner(repo.id)
            proj["owner"] = email
            user_name = get_user_name(email)
            if user_name != email:
                proj["owner_name"] = user_name 
            proj["in_progress"] = True

            commits = get_commits(repo.id, 0, 1)
            commit = get_commit(repo.id, repo.version, commits[0].id)
            dir = fs_mgr.load_seafdir(repo.id, repo.version, commit.root_id)
            file = dir.lookup(ARCHIVE_METADATA_TARGET)
            if file:
                md = parse_markdown(file.get_content())
                if md:
                    # Author
                    a = md.get("Author")
                    if a:
                        a_list = strip_uni(a.strip()).split('\n')
                        authors = []
                        for _ in a_list:
                            author = {}
                            aa = _.split(';')
                            author['name'] = aa[0]
                            if len(aa) > 1 and aa[1].strip():
                                author['affs'] = [x.strip() for x in aa[1].split('|')]
                                author['affs'] = [x for x in author['affs'] if x ]
                            authors.append(author)
                        if a:
                            proj["authors"] = authors

                    # Description
                    d = strip_uni(md.get("Description"))
                    if d:
                        proj["description"] = d

                    # Comments
                    c = strip_uni(md.get("Comments"))
                    if c:
                        proj["comments"] = c

                    #Title
                    t = strip_uni(md.get("Title"))
                    if t:
                        proj["title"] = t
                        del proj["in_progress"]
                    
                    proj["is_certified"] = is_certified_by_repo_id(repo.id)
            else:
                if DEBUG:
                    print "No %s for repo %s found" % (ARCHIVE_METADATA_TARGET, repo.name)
            catalog.append(proj)    

        except Exception as err:
            msg = "repo_name: %s, id: %s, err: %s" % ( repo.name, repo.id, str(err) ) 
            logging.error (msg)
            if DEBUG:
                print msg

    return catalog