Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add last_modify field in form #666

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/rpc-service.c
Original file line number Diff line number Diff line change
Expand Up @@ -2569,6 +2569,7 @@ seafile_post_multi_files (const char *repo_id,
paths_json,
user,
replace_existed,
0,
&ret_json,
NULL,
error);
Expand Down
19 changes: 14 additions & 5 deletions fileserver/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,12 @@ 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 != "" {
lastModify, _ = strconv.ParseInt(lastModifyStr, 10, 64)
}

relativePath := normalizeUTF8Path(r.FormValue("relative_path"))
if relativePath != "" {
if relativePath[0] == '/' || relativePath[0] == '\\' {
Expand Down Expand Up @@ -1114,7 +1120,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 @@ -1459,7 +1465,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 @@ -1527,7 +1533,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, lastModify, ids, sizes)
if err != nil {
err := fmt.Errorf("failed to post files and gen commit: %v", err)
return &appError{err, "", http.StatusInternalServerError}
Expand Down Expand Up @@ -1583,7 +1589,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, lastModify int64, ids []string, sizes []int64) (string, error) {
repo := repomgr.Get(repoID)
if repo == nil {
err := fmt.Errorf("failed to get repo %s", repoID)
Expand All @@ -1603,7 +1609,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
1 change: 1 addition & 0 deletions server/index-blocks-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ start_index_task (gpointer data, gpointer user_data)
idx_para->user,
idx_para->ret_json ? &ret_json : NULL,
idx_para->replace_existed,
0,
idx_para->canon_path,
id_list,
size_list,
Expand Down
2 changes: 2 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 @@ -911,6 +912,7 @@ post_files_and_gen_commit (GList *filenames,
const char *user,
char **ret_json,
int replace_existed,
gint64 mtime,
const char *canon_path,
GList *id_list,
GList *size_list,
Expand Down
13 changes: 11 additions & 2 deletions server/repo-op.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ post_files_and_gen_commit (GList *filenames,
const char *user,
char **ret_json,
int replace_existed,
gint64 mtime,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个参数应该放在最后。

const char *canon_path,
GList *id_list,
GList *size_list,
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 @@ -1166,6 +1173,7 @@ seaf_repo_manager_post_multi_files (SeafRepoManager *mgr,
user,
ret_json,
replace_existed,
mtime,
canon_path,
id_list,
size_list,
Expand Down Expand Up @@ -1204,6 +1212,7 @@ post_files_and_gen_commit (GList *filenames,
const char *user,
char **ret_json,
int replace_existed,
gint64 mtime,
const char *canon_path,
GList *id_list,
GList *size_list,
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
16 changes: 16 additions & 0 deletions server/upload-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ upload_api_cb(evhtp_request_t *req, void *arg)
RecvFSM *fsm = arg;
char *parent_dir, *replace_str;
char *relative_path = NULL, *new_parent_dir = NULL;
char *last_modify = NULL;
gint64 mtime = 0;
GError *error = NULL;
int error_code = -1;
char *filenames_json, *tmp_files_json;
Expand Down Expand Up @@ -468,6 +470,11 @@ upload_api_cb(evhtp_request_t *req, void *arg)
return;
}

last_modify = g_hash_table_lookup (fsm->form_kvs, "last_modify");
if (last_modify) {
mtime = atoll(last_modify);
}

replace_str = g_hash_table_lookup (fsm->form_kvs, "replace");
if (replace_str) {
replace = atoi(replace_str);
Expand Down Expand Up @@ -575,6 +582,7 @@ upload_api_cb(evhtp_request_t *req, void *arg)
tmp_files_json,
fsm->user,
replace,
mtime,
&ret_json,
fsm->need_idx_progress ? &task_id : NULL,
&error);
Expand Down Expand Up @@ -1051,6 +1059,8 @@ upload_ajax_cb(evhtp_request_t *req, void *arg)
{
RecvFSM *fsm = arg;
char *parent_dir = NULL, *relative_path = NULL, *new_parent_dir = NULL;
char *last_modify = NULL;
gint64 mtime = 0;
GError *error = NULL;
int error_code = -1;
char *filenames_json, *tmp_files_json;
Expand Down Expand Up @@ -1091,6 +1101,11 @@ upload_ajax_cb(evhtp_request_t *req, void *arg)
return;
}

last_modify = g_hash_table_lookup (fsm->form_kvs, "last_modify");
if (last_modify) {
mtime = atoll(last_modify);
}

if (!fsm->filenames) {
seaf_debug ("[upload] No file uploaded.\n");
send_error_reply (req, EVHTP_RES_BADREQ, "No file uploaded.\n");
Expand Down Expand Up @@ -1190,6 +1205,7 @@ upload_ajax_cb(evhtp_request_t *req, void *arg)
tmp_files_json,
fsm->user,
0,
mtime,
&ret_json,
fsm->need_idx_progress ? &task_id : NULL,
&error);
Expand Down
44 changes: 44 additions & 0 deletions tests/test_file_operation/test_upload_and_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,47 @@ def test_api(repo):
time.sleep(1)
del_repo_files(repo.id)
del_local_files()

def test_ajax_mtime(repo):
create_test_file()
obj_id = '{"parent_dir":"/"}'
mtime = '1695809905'

token = api.get_fileserver_access_token(repo.id, obj_id, 'upload', USER, False)
upload_url_base = 'http://127.0.0.1:8082/upload-aj/'+ token
m = MultipartEncoder(
fields={
'parent_dir': '/',
'last_modify': mtime,
'file': (file_name, open(file_path, 'rb'), 'application/octet-stream')
})
response = requests.post(upload_url_base,
data = m, headers = {'Content-Type': m.content_type})
assert_upload_response(response, False, False)

dent = api.get_dirent_by_path(repo.id, '/' + file_name)

assert dent.mtime == 1695809905

def test_api_mtime(repo):
create_test_file()
params = {'ret-json':'1'}
obj_id = '{"parent_dir":"/"}'
mtime = '1695809905'

token = api.get_fileserver_access_token(repo.id, obj_id, 'upload', USER, False)
upload_url_base = 'http://127.0.0.1:8082/upload-api/' + token
m = MultipartEncoder(
fields={
'parent_dir': '/',
'last_modify': mtime,
'file': (file_name, open(file_path, 'rb'), 'application/octet-stream')
})
response = requests.post(upload_url_base, params = params,
data = m, headers = {'Content-Type': m.content_type})
assert_upload_response(response, False, False)

dent = api.get_dirent_by_path(repo.id, '/' + file_name)

assert dent.mtime == 1695809905

Loading