web界面上传文件,python里面返回一个请求c服务的url,继而请求c,用的是libevhtp
static void
upload_ajax_cb(evhtp_request_t *req, void *arg)
{
RecvFSM *fsm = arg;
SearpcClient *rpc_client = NULL;
char *parent_dir;
GError *error = NULL;
int error_code = ERROR_INTERNAL;
char *filenames_json, *tmp_files_json;
evhtp_headers_add_header (req->headers_out,
evhtp_header_new("Access-Control-Allow-Headers",
"x-requested-with, content-type, accept, origin, authorization", 1, 1));
evhtp_headers_add_header (req->headers_out,
evhtp_header_new("Access-Control-Allow-Methods",
"GET, POST, PUT, PATCH, DELETE, OPTIONS", 1, 1));
evhtp_headers_add_header (req->headers_out,
evhtp_header_new("Access-Control-Allow-Origin",
"*", 1, 1));
evhtp_headers_add_header (req->headers_out,
evhtp_header_new("Access-Control-Max-Age",
"86400", 1, 1));
evhtp_headers_add_header (req->headers_out,
evhtp_header_new("Content-Type",
"application/json; charset=utf-8", 1, 1));
if (evhtp_request_get_method(req) == htp_method_OPTIONS) {
set_content_length_header (req);
evhtp_send_reply (req, EVHTP_RES_OK);
return;
}
if (!fsm || fsm->state == RECV_ERROR)
return;
if (!fsm->files) {
seaf_warning ("[upload] No file uploaded.\n");
set_content_length_header (req);
evhtp_send_reply (req, EVHTP_RES_BADREQ);
return;
}
parent_dir = g_hash_table_lookup (fsm->form_kvs, "parent_dir");
if (!parent_dir) {
seaf_warning ("[upload] No parent dir given.\n");
evbuffer_add_printf(req->buffer_out, "Invalid URL.\n");
set_content_length_header (req);
evhtp_send_reply (req, EVHTP_RES_BADREQ);
return;
}
if (!check_tmp_file_list (fsm->files, &error_code))
goto error;
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 ("[upload] Out of quota.\n");
error_code = ERROR_QUOTA;
goto error;
}
filenames_json = file_list_to_json (fsm->filenames);
tmp_files_json = file_list_to_json (fsm->files);
char *ret_json = seafile_post_multi_files (rpc_client,
fsm->repo_id,
parent_dir,
filenames_json,
tmp_files_json,
fsm->user,
0,
&error);
g_free (filenames_json);
g_free (tmp_files_json);
if (error) {
if (error->code == POST_FILE_ERR_FILENAME) {
error_code = ERROR_FILENAME;
seaf_warning ("[upload] Bad filename.\n");
}
g_clear_error (&error);
goto error;
}
ccnet_rpc_client_free (rpc_client);
evbuffer_add (req->buffer_out, ret_json, strlen(ret_json));
g_free (ret_json);
set_content_length_header (req);
evhtp_send_reply (req, EVHTP_RES_OK);
return;
error:
if (rpc_client)
ccnet_rpc_client_free (rpc_client);
switch (error_code) {
case ERROR_FILENAME:
evbuffer_add_printf(req->buffer_out, "{\"error\": \"Invalid filename.\"}");
set_content_length_header (req);
evhtp_send_reply (req, SEAF_HTTP_RES_BADFILENAME);
break;
case ERROR_EXISTS:
evbuffer_add_printf(req->buffer_out, "{\"error\": \"File already exists.\"}");
set_content_length_header (req);
evhtp_send_reply (req, SEAF_HTTP_RES_EXISTS);
break;
case ERROR_SIZE:
evbuffer_add_printf(req->buffer_out, "{\"error\": \"File size is too large.\"}");
set_content_length_header (req);
evhtp_send_reply (req, SEAF_HTTP_RES_TOOLARGE);
break;
case ERROR_QUOTA:
evbuffer_add_printf(req->buffer_out, "{\"error\": \"Out of quota.\"}");
set_content_length_header (req);
evhtp_send_reply (req, SEAF_HTTP_RES_NOQUOTA);
break;
case ERROR_RECV:
case ERROR_INTERNAL:
set_content_length_header (req);
evhtp_send_reply (req, EVHTP_RES_SERVERR);
break;
}
}
def get_file_op_url(request, repo_id):
"""Get file upload/update url for AJAX.
"""
content_type = 'application/json; charset=utf-8'
op_type = request.GET.get('op_type') # value can be 'upload', 'update', 'upload-blks', 'update-blks'
if not op_type:
err_msg = _(u'Argument missing')
return HttpResponse(json.dumps({"error": err_msg}), status=400,
content_type=content_type)
username = request.user.username
url = ''
if check_repo_access_permission(repo_id, request.user) == 'rw':
token = seafile_api.get_fileserver_access_token(repo_id, 'dummy',
op_type, username)
url = gen_file_upload_url(token, op_type + '-aj')
return HttpResponse(json.dumps({"url": url}), content_type=content_type)
例如
192.168.1.10:8082是c的fileserver服务
8082被fileserver二进制程序监听
我的问题是这里的headers_out是返回给谁的,是浏览器的web客户端,还是SearpcClient这里自定义的client
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论