Skip to content

Commit

Permalink
Add last_modify field in form (#666)
Browse files Browse the repository at this point in the history
* Add last_modify field in form

* Go add last_modify field in form

* Use rfc3339 time format

* Add mtime for upload_blks_api

---------

Co-authored-by: 杨赫然 <[email protected]>
  • Loading branch information
feiniks and 杨赫然 authored Jul 23, 2024
1 parent de3ddbc commit 6c056c1
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 13 deletions.
2 changes: 2 additions & 0 deletions common/rpc-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -2548,6 +2548,7 @@ seafile_post_multi_files (const char *repo_id,
paths_json,
user,
replace_existed,
0,
&ret_json,
NULL,
error);
Expand Down Expand Up @@ -2598,6 +2599,7 @@ seafile_put_file (const char *repo_id, const char *temp_file_path,
seaf_repo_manager_put_file (seaf->repo_mgr, repo_id,
temp_file_path, rpath,
norm_file_name, user, head_id,
0,
&new_file_id, error);

out:
Expand Down
54 changes: 45 additions & 9 deletions fileserver/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,15 @@ func doUpload(rsp http.ResponseWriter, r *http.Request, fsm *recvData, isAjax bo
return &appError{nil, msg, http.StatusBadRequest}
}

lastModifyStr := normalizeUTF8Path(r.FormValue("last_modify"))
var lastModify int64
if lastModifyStr != "" {
t, err := time.Parse(time.RFC3339, lastModifyStr)
if err == nil {
lastModify = t.Unix()
}
}

relativePath := normalizeUTF8Path(r.FormValue("relative_path"))
if relativePath != "" {
if relativePath[0] == '/' || relativePath[0] == '\\' {
Expand Down Expand Up @@ -1115,7 +1124,7 @@ func doUpload(rsp http.ResponseWriter, r *http.Request, fsm *recvData, isAjax bo
}

if err := postMultiFiles(rsp, r, repoID, newParentDir, user, fsm,
replaceExisted, isAjax); err != nil {
replaceExisted, lastModify, isAjax); err != nil {
return err
}

Expand Down Expand Up @@ -1460,7 +1469,7 @@ func parseUploadHeaders(r *http.Request) (*recvData, *appError) {
return fsm, nil
}

func postMultiFiles(rsp http.ResponseWriter, r *http.Request, repoID, parentDir, user string, fsm *recvData, replace bool, isAjax bool) *appError {
func postMultiFiles(rsp http.ResponseWriter, r *http.Request, repoID, parentDir, user string, fsm *recvData, replace bool, lastModify int64, isAjax bool) *appError {

fileNames := fsm.fileNames
files := fsm.files
Expand Down Expand Up @@ -1528,7 +1537,7 @@ func postMultiFiles(rsp http.ResponseWriter, r *http.Request, repoID, parentDir,
}
}

retStr, err := postFilesAndGenCommit(fileNames, repo.ID, user, canonPath, replace, ids, sizes)
retStr, err := postFilesAndGenCommit(fileNames, repo.ID, user, canonPath, replace, ids, sizes, lastModify)
if err != nil {
err := fmt.Errorf("failed to post files and gen commit: %v", err)
return &appError{err, "", http.StatusInternalServerError}
Expand Down Expand Up @@ -1584,7 +1593,7 @@ func checkFilesWithSameName(repo *repomgr.Repo, canonPath string, fileNames []st
return false
}

func postFilesAndGenCommit(fileNames []string, repoID string, user, canonPath string, replace bool, ids []string, sizes []int64) (string, error) {
func postFilesAndGenCommit(fileNames []string, repoID string, user, canonPath string, replace bool, ids []string, sizes []int64, lastModify int64) (string, error) {
repo := repomgr.Get(repoID)
if repo == nil {
err := fmt.Errorf("failed to get repo %s", repoID)
Expand All @@ -1604,7 +1613,10 @@ func postFilesAndGenCommit(fileNames []string, repoID string, user, canonPath st
break
}
mode := (syscall.S_IFREG | 0644)
mtime := time.Now().Unix()
mtime := lastModify
if mtime <= 0 {
mtime = time.Now().Unix()
}
dent := fsmgr.NewDirent(ids[i], name, uint32(mode), mtime, "", sizes[i])
dents = append(dents, dent)
}
Expand Down Expand Up @@ -2763,6 +2775,15 @@ func doUpdate(rsp http.ResponseWriter, r *http.Request, fsm *recvData, isAjax bo
return &appError{nil, msg, http.StatusBadRequest}
}

lastModifyStr := normalizeUTF8Path(r.FormValue("last_modify"))
var lastModify int64
if lastModifyStr != "" {
t, err := time.Parse(time.RFC3339, lastModifyStr)
if err == nil {
lastModify = t.Unix()
}
}

parentDir := filepath.Dir(targetFile)
fileName := filepath.Base(targetFile)

Expand Down Expand Up @@ -2869,7 +2890,7 @@ func doUpdate(rsp http.ResponseWriter, r *http.Request, fsm *recvData, isAjax bo
headID = headIDs[0]
}

if err := putFile(rsp, r, repoID, parentDir, user, fileName, fsm, headID, isAjax); err != nil {
if err := putFile(rsp, r, repoID, parentDir, user, fileName, fsm, headID, lastModify, isAjax); err != nil {
return err
}

Expand All @@ -2879,7 +2900,7 @@ func doUpdate(rsp http.ResponseWriter, r *http.Request, fsm *recvData, isAjax bo
return nil
}

func putFile(rsp http.ResponseWriter, r *http.Request, repoID, parentDir, user, fileName string, fsm *recvData, headID string, isAjax bool) *appError {
func putFile(rsp http.ResponseWriter, r *http.Request, repoID, parentDir, user, fileName string, fsm *recvData, headID string, lastModify int64, isAjax bool) *appError {
files := fsm.files
repo := repomgr.Get(repoID)
if repo == nil {
Expand Down Expand Up @@ -2974,6 +2995,9 @@ func putFile(rsp http.ResponseWriter, r *http.Request, repoID, parentDir, user,
}

mtime := time.Now().Unix()
if lastModify > 0 {
mtime = lastModify
}
mode := (syscall.S_IFREG | 0644)
newDent := fsmgr.NewDirent(fileID, fileName, uint32(mode), mtime, user, size)

Expand Down Expand Up @@ -3086,6 +3110,15 @@ func doUploadBlks(rsp http.ResponseWriter, r *http.Request, fsm *recvData) *appE
return &appError{nil, msg, http.StatusBadRequest}
}

lastModifyStr := normalizeUTF8Path(r.FormValue("last_modify"))
var lastModify int64
if lastModifyStr != "" {
t, err := time.Parse(time.RFC3339, lastModifyStr)
if err == nil {
lastModify = t.Unix()
}
}

fileName := normalizeUTF8Path(r.FormValue("file_name"))
if fileName == "" {
msg := "No file_name given.\n"
Expand Down Expand Up @@ -3124,7 +3157,7 @@ func doUploadBlks(rsp http.ResponseWriter, r *http.Request, fsm *recvData) *appE
return &appError{nil, msg, http.StatusBadRequest}
}

fileID, appErr := commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user, fileSize, replaceExisted)
fileID, appErr := commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user, fileSize, replaceExisted, lastModify)
if appErr != nil {
return appErr
}
Expand All @@ -3150,7 +3183,7 @@ func doUploadBlks(rsp http.ResponseWriter, r *http.Request, fsm *recvData) *appE
return nil
}

func commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user string, fileSize int64, replace bool) (string, *appError) {
func commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user string, fileSize int64, replace bool, lastModify int64) (string, *appError) {
repo := repomgr.Get(repoID)
if repo == nil {
msg := "Failed to get repo.\n"
Expand Down Expand Up @@ -3195,6 +3228,9 @@ func commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user string, fi
}

mtime := time.Now().Unix()
if lastModify > 0 {
mtime = lastModify
}
mode := (syscall.S_IFREG | 0644)
newDent := fsmgr.NewDirent(fileID, fileName, uint32(mode), mtime, user, fileSize)
var names []string
Expand Down
1 change: 1 addition & 0 deletions server/index-blocks-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ start_index_task (gpointer data, gpointer user_data)
idx_para->canon_path,
id_list,
size_list,
0,
NULL);
progress->status = ret;
if (idx_para->ret_json) {
Expand Down
4 changes: 4 additions & 0 deletions server/repo-mgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ seaf_repo_manager_post_multi_files (SeafRepoManager *mgr,
const char *paths_json,
const char *user,
int replace_existed,
gint64 mtime,
char **new_ids,
char **task_id,
GError **error);
Expand Down Expand Up @@ -363,6 +364,7 @@ seaf_repo_manager_commit_file_blocks (SeafRepoManager *mgr,
const char *user,
gint64 file_size,
int replace_existed,
gint64 mtime,
char **new_id,
GError **error);

Expand Down Expand Up @@ -405,6 +407,7 @@ seaf_repo_manager_put_file (SeafRepoManager *mgr,
const char *file_name,
const char *user,
const char *head_id,
gint64 mtime,
char **new_file_id,
GError **error);

Expand Down Expand Up @@ -916,6 +919,7 @@ post_files_and_gen_commit (GList *filenames,
const char *canon_path,
GList *id_list,
GList *size_list,
gint64 mtime,
GError **error);

char *
Expand Down
25 changes: 21 additions & 4 deletions server/repo-op.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ post_files_and_gen_commit (GList *filenames,
const char *canon_path,
GList *id_list,
GList *size_list,
gint64 mtime,
GError **error);

/*
Expand Down Expand Up @@ -904,6 +905,7 @@ do_post_multi_files (SeafRepo *repo,
GList *size_list,
const char *user,
int replace_existed,
gint64 mtime,
GList **name_list)
{
SeafDirent *dent;
Expand All @@ -925,7 +927,11 @@ do_post_multi_files (SeafRepo *repo,
dent->id[40] = '\0';
dent->size = *size;
dent->mode = STD_FILE_MODE;
dent->mtime = (gint64)time(NULL);
if (mtime > 0) {
dent->mtime = mtime;
} else {
dent->mtime = (gint64)time(NULL);
}

dents = g_list_append (dents, dent);
}
Expand Down Expand Up @@ -1069,6 +1075,7 @@ seaf_repo_manager_post_multi_files (SeafRepoManager *mgr,
const char *paths_json,
const char *user,
int replace_existed,
gint64 mtime,
char **ret_json,
char **task_id,
GError **error)
Expand Down Expand Up @@ -1169,6 +1176,7 @@ seaf_repo_manager_post_multi_files (SeafRepoManager *mgr,
canon_path,
id_list,
size_list,
mtime,
error);
} else {
ret = index_blocks_mgr_start_index (seaf->index_blocks_mgr,
Expand Down Expand Up @@ -1207,6 +1215,7 @@ post_files_and_gen_commit (GList *filenames,
const char *canon_path,
GList *id_list,
GList *size_list,
gint64 mtime,
GError **error)
{
SeafRepo *repo = NULL;
Expand All @@ -1224,7 +1233,7 @@ post_files_and_gen_commit (GList *filenames,
/* Add the files to parent dir and commit. */
root_id = do_post_multi_files (repo, head_commit->root_id, canon_path,
filenames, id_list, size_list, user,
replace_existed, &name_list);
replace_existed, mtime, &name_list);
if (!root_id) {
seaf_warning ("[post multi-file] Failed to post files to %s in repo %s.\n",
canon_path, repo->id);
Expand Down Expand Up @@ -1496,6 +1505,7 @@ seaf_repo_manager_commit_file_blocks (SeafRepoManager *mgr,
const char *user,
gint64 file_size,
int replace_existed,
gint64 mtime,
char **new_id,
GError **error)
{
Expand Down Expand Up @@ -1554,9 +1564,12 @@ seaf_repo_manager_commit_file_blocks (SeafRepoManager *mgr,
}

rawdata_to_hex(sha1, hex, 20);
if (mtime <= 0) {
mtime = (gint64)time(NULL);
}
new_dent = seaf_dirent_new (dir_version_from_repo_version(repo->version),
hex, STD_FILE_MODE, file_name,
(gint64)time(NULL), user, file_size);
mtime, user, file_size);

root_id = do_post_file_replace (repo, head_commit->root_id,
canon_path, replace_existed, new_dent);
Expand Down Expand Up @@ -4120,6 +4133,7 @@ seaf_repo_manager_put_file (SeafRepoManager *mgr,
const char *file_name,
const char *user,
const char *head_id,
gint64 mtime,
char **new_file_id,
GError **error)
{
Expand Down Expand Up @@ -4197,9 +4211,12 @@ seaf_repo_manager_put_file (SeafRepoManager *mgr,
}

rawdata_to_hex(sha1, hex, 20);
if (mtime <= 0) {
mtime = (gint64)time(NULL);
}
new_dent = seaf_dirent_new (dir_version_from_repo_version(repo->version),
hex, STD_FILE_MODE, file_name,
(gint64)time(NULL), user, size);
mtime, user, size);

if (!fullpath)
fullpath = g_build_filename(parent_dir, file_name, NULL);
Expand Down
Loading

0 comments on commit 6c056c1

Please sign in to comment.