/
fdfs_upload_file.c
93 lines (82 loc) · 2.1 KB
/
fdfs_upload_file.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "fdfs_client.h"
#include "logger.h"
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
int upload(lua_State *L)
{
char *conf_filename;
char *local_filename;
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
ConnectionInfo *pTrackerServer;
int result;
int store_path_index;
ConnectionInfo storageServer;
char file_id[128];
size_t n;
char response[255];
conf_filename = luaL_checklstring(L, 1, &n);
local_filename = luaL_checklstring(L, 2, &n);
log_init();
g_log_context.log_level = LOG_ERR;
ignore_signal_pipe();
if ((result=fdfs_client_init(conf_filename)) != 0)
{
return result;
}
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
fdfs_client_destroy();
return errno != 0 ? errno : ECONNREFUSED;
}
*group_name = '\0';
if ((result=tracker_query_storage_store(pTrackerServer, \
&storageServer, group_name, &store_path_index)) != 0)
{
fdfs_client_destroy();
return result;
}
result = storage_upload_by_filename1(pTrackerServer, \
&storageServer, store_path_index, \
local_filename, NULL, \
NULL, 0, group_name, file_id);
if (result == 0)
{
sprintf(response, "http://%s/%s\n", storageServer.ip_addr, file_id);
lua_pushstring(L, response);
}
tracker_disconnect_server_ex(pTrackerServer, true);
fdfs_client_destroy();
if (result == 0)
return 1;
return result;
}
static luaL_Reg mylibs[] = {
{"fastdfs_upload", upload},
{NULL, NULL}
};
int luaopen_fastdfs(lua_State* L)
{
const char* libName = "fastdfs";
luaL_register(L,libName, mylibs);
return 1;
}
//int main() {
// upload("/etc/fdfs/client.conf", "/home/yuecaili/Lighthouse.jpg");
// return 0;
//}