static int put_file (int argc, char **argv) { GError *error = NULL; const char *repo_id; const char *file_path; const char *parent_dir; const char *file_name; const char *user; if (argc != 5) { fprintf (stderr, "[usage] seafserv-tool put-file <repo id> <file>" " <parent dir> <file name> <user> \n"); return -1; } repo_id = argv[0]; file_path = argv[1]; parent_dir = argv[2]; file_name = argv[3]; user = argv[4]; seafile_put_file(threaded_rpc_client, repo_id, file_path, parent_dir, file_name, user, NULL, &error); if (error) { fprintf (stderr, "Failed to put a file into server (filepath %s)\n", file_path); if (error && error->message) fprintf (stderr, "Error: %s\n", error->message); return -1; } printf ("Success to put a file into server\n"); return 0; }
static void update_cb(evhtp_request_t *req, void *arg) { RecvFSM *fsm = arg; SearpcClient *rpc_client = NULL; char *target_file, *parent_dir = NULL, *filename = NULL; const char *head_id = NULL; GError *error = NULL; int error_code = ERROR_INTERNAL; if (!fsm || fsm->state == RECV_ERROR) return; if (!fsm->tmp_files) { seaf_warning ("[update] No file uploaded.\n"); evhtp_send_reply (req, EVHTP_RES_BADREQ); return; } target_file = g_hash_table_lookup (fsm->form_kvs, "target_file"); if (!target_file) { seaf_warning ("[Update] No target file given.\n"); evbuffer_add_printf(req->buffer_out, "Invalid URL.\n"); evhtp_send_reply (req, EVHTP_RES_BADREQ); return; } parent_dir = g_path_get_dirname (target_file); filename = g_path_get_basename (target_file); if (!check_tmp_file_list (fsm->tmp_files, &error_code)) goto error; head_id = evhtp_kv_find (req->uri->query, "head"); rpc_client = ccnet_create_pooled_rpc_client (seaf->client_pool, NULL, "seafserv-threaded-rpcserver"); if (seafile_check_quota (rpc_client, fsm->repo_id, NULL) < 0) { seaf_warning ("[update] Out of quota.\n"); error_code = ERROR_QUOTA; goto error; } seafile_put_file (rpc_client, fsm->repo_id, (char *)(fsm->tmp_files->data), parent_dir, filename, fsm->user, head_id, &error); if (error) { if (g_strcmp0 (error->message, "file does not exist") == 0) { error_code = ERROR_NOT_EXIST; } if (error->message) printf ("%s\n", error->message); g_clear_error (&error); goto error; } ccnet_rpc_client_free (rpc_client); /* Redirect to repo dir page after upload finishes. */ redirect_to_success_page (req, fsm->repo_id, parent_dir); g_free (parent_dir); g_free (filename); return; error: if (rpc_client) ccnet_rpc_client_free (rpc_client); redirect_to_update_error (req, fsm->repo_id, target_file, error_code); g_free (parent_dir); g_free (filename); }
static void update_api_cb(evhtp_request_t *req, void *arg) { RecvFSM *fsm = arg; SearpcClient *rpc_client = NULL; char *target_file, *parent_dir = NULL, *filename = NULL; const char *head_id = NULL; GError *error = NULL; int error_code = ERROR_INTERNAL; char *new_file_id = NULL; if (!fsm || fsm->state == RECV_ERROR) return; if (!fsm->files) { seaf_warning ("[update] No file uploaded.\n"); set_content_length_header (req); evhtp_send_reply (req, EVHTP_RES_BADREQ); return; } target_file = g_hash_table_lookup (fsm->form_kvs, "target_file"); if (!target_file) { seaf_warning ("[Update] No target file given.\n"); evbuffer_add_printf(req->buffer_out, "Invalid URL.\n"); set_content_length_header (req); evhtp_send_reply (req, EVHTP_RES_BADREQ); return; } parent_dir = g_path_get_dirname (target_file); filename = g_path_get_basename (target_file); if (!check_tmp_file_list (fsm->files, &error_code)) goto error; head_id = evhtp_kv_find (req->uri->query, "head"); rpc_client = ccnet_create_pooled_rpc_client (seaf->client_pool, NULL, "seafserv-threaded-rpcserver"); if (seafile_check_quota (rpc_client, fsm->repo_id, NULL) < 0) { seaf_warning ("[update] Out of quota.\n"); error_code = ERROR_QUOTA; goto error; } new_file_id = seafile_put_file (rpc_client, fsm->repo_id, (char *)(fsm->files->data), parent_dir, filename, fsm->user, head_id, &error); g_free (parent_dir); g_free (filename); if (error) { if (g_strcmp0 (error->message, "file does not exist") == 0) { error_code = ERROR_NOT_EXIST; } if (error->message) seaf_warning ("%s\n", error->message); g_clear_error (&error); goto error; } ccnet_rpc_client_free (rpc_client); /* Send back the new file id, so that the mobile client can update local cache */ evbuffer_add(req->buffer_out, new_file_id, strlen(new_file_id)); set_content_length_header (req); evhtp_send_reply (req, EVHTP_RES_OK); g_free (new_file_id); return; error: if (rpc_client) ccnet_rpc_client_free (rpc_client); switch (error_code) { case ERROR_FILENAME: evbuffer_add_printf(req->buffer_out, "Invalid filename.\n"); set_content_length_header (req); evhtp_send_reply (req, SEAF_HTTP_RES_BADFILENAME); break; case ERROR_EXISTS: evbuffer_add_printf(req->buffer_out, "File already exists.\n"); set_content_length_header (req); evhtp_send_reply (req, SEAF_HTTP_RES_EXISTS); break; case ERROR_SIZE: evbuffer_add_printf(req->buffer_out, "File size is too large.\n"); set_content_length_header (req); evhtp_send_reply (req, SEAF_HTTP_RES_TOOLARGE); break; case ERROR_QUOTA: evbuffer_add_printf(req->buffer_out, "Out of quota.\n"); set_content_length_header (req); evhtp_send_reply (req, SEAF_HTTP_RES_NOQUOTA); break; case ERROR_NOT_EXIST: evbuffer_add_printf(req->buffer_out, "File does not exist.\n"); set_content_length_header (req); evhtp_send_reply (req, SEAF_HTTP_RES_NOT_EXISTS); break; case ERROR_RECV: case ERROR_INTERNAL: default: set_content_length_header (req); evhtp_send_reply (req, EVHTP_RES_SERVERR); break; } }