From 5caa19235abe65315f616c4aa838f1151657eed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Tue, 16 Jul 2024 14:33:39 +0800 Subject: [PATCH] Add last_modify field in form --- common/rpc-service.c | 1 + server/index-blocks-mgr.c | 1 + server/repo-mgr.h | 2 + server/repo-op.c | 13 +++++- server/upload-file.c | 16 +++++++ .../test_upload_and_update.py | 44 +++++++++++++++++++ 6 files changed, 75 insertions(+), 2 deletions(-) diff --git a/common/rpc-service.c b/common/rpc-service.c index 671294eb..a64acad2 100644 --- a/common/rpc-service.c +++ b/common/rpc-service.c @@ -2569,6 +2569,7 @@ seafile_post_multi_files (const char *repo_id, paths_json, user, replace_existed, + 0, &ret_json, NULL, error); diff --git a/server/index-blocks-mgr.c b/server/index-blocks-mgr.c index 32cfbe60..a24db603 100644 --- a/server/index-blocks-mgr.c +++ b/server/index-blocks-mgr.c @@ -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, diff --git a/server/repo-mgr.h b/server/repo-mgr.h index 7237c47c..a23f21b1 100644 --- a/server/repo-mgr.h +++ b/server/repo-mgr.h @@ -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); @@ -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, diff --git a/server/repo-op.c b/server/repo-op.c index 5f1916f6..83741642 100644 --- a/server/repo-op.c +++ b/server/repo-op.c @@ -48,6 +48,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, @@ -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; @@ -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); } @@ -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) @@ -1166,6 +1173,7 @@ seaf_repo_manager_post_multi_files (SeafRepoManager *mgr, user, ret_json, replace_existed, + mtime, canon_path, id_list, size_list, @@ -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, @@ -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); diff --git a/server/upload-file.c b/server/upload-file.c index 7ba17656..3ad53a4d 100755 --- a/server/upload-file.c +++ b/server/upload-file.c @@ -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; @@ -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); @@ -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); @@ -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; @@ -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"); @@ -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); diff --git a/tests/test_file_operation/test_upload_and_update.py b/tests/test_file_operation/test_upload_and_update.py index 0727ede6..94d64223 100644 --- a/tests/test_file_operation/test_upload_and_update.py +++ b/tests/test_file_operation/test_upload_and_update.py @@ -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 +