1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 
5 #include "amp_config.h"
6 #include "aos_system.h"
7 #include "amp_defines.h"
8 #include "quickjs.h"
9 #include "quickjs_addon_common.h"
10 #include "ulog/ulog.h"
11 #include "oss_app.h"
12 
13 #define MOD_STR "MOD_OSS"
14 #define MAX_URL_LENGTH 256
15 
16 static aos_sem_t g_oss_task_sem = NULL;
17 static JSClassID js_oss_class_id;
18 static char oss_resp_url[MAX_URL_LENGTH];
19 
20 typedef struct {
21     int flag;   //0: upload; 1: download;
22     char *key;
23     char *secret;
24     char *endPoint;
25     char *bucketName;
26     char *objectName;
27     char *filePath;
28 }  oss_task_param;
29 
oss_task_handler(void * arg)30 static void oss_task_handler(void *arg)
31 {
32     char *url = NULL;
33     oss_task_param *task_param = (oss_task_param *)arg;
34 
35     if(task_param->flag == 0) {
36         url = oss_upload_file(task_param->key, task_param->secret, task_param->endPoint, task_param->bucketName, task_param->objectName, task_param->filePath);
37         memset(oss_resp_url, 0, MAX_URL_LENGTH);
38         strncpy(oss_resp_url, url, MAX_URL_LENGTH);
39     } else {
40         oss_download_file(task_param->key, task_param->secret, task_param->endPoint, task_param->bucketName, task_param->objectName, task_param->filePath);
41     }
42     aos_printf("oss task finished! \n");
43 
44     aos_sem_signal(&g_oss_task_sem);
45 }
46 
native_oss_upload_file(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)47 static JSValue native_oss_upload_file(JSContext *ctx, JSValueConst this_val,
48                           int argc, JSValueConst *argv)
49 {
50     int ret = -1;
51 
52     aos_task_t oss_task;
53     oss_task_param task_param;
54 
55     if (argc < 6)
56     {
57         amp_error(MOD_STR, "%s: args num is illegal :n_args = %d;\n", __func__, argc);
58         return JS_NewInt32(ctx, 0);
59     }
60 
61     task_param.flag = 0;
62     task_param.key = (char *)JS_ToCString(ctx, argv[0]);
63     task_param.secret = (char *)JS_ToCString(ctx, argv[1]);
64     task_param.endPoint = (char *)JS_ToCString(ctx, argv[2]);
65     task_param.bucketName = (char *)JS_ToCString(ctx, argv[3]);
66     task_param.objectName = (char *)JS_ToCString(ctx, argv[4]);
67     task_param.filePath = (char *)JS_ToCString(ctx, argv[5]);
68 
69     // amp_debug(MOD_STR, "key = %s;\n", task_param.key);
70     // amp_debug(MOD_STR, "secret = %s;\n", task_param.secret);
71     amp_debug(MOD_STR, "endPoint = %s;\n", task_param.endPoint);
72     amp_debug(MOD_STR, "bucketName = %s;\n", task_param.bucketName);
73     amp_debug(MOD_STR, "objectName = %s;\n", task_param.objectName);
74     amp_debug(MOD_STR, "filePath = %s;\n", task_param.filePath);
75 
76     aos_task_new_ext(&oss_task, "amp oss task", oss_task_handler, &task_param, 1024 * 10, AOS_DEFAULT_APP_PRI);
77     aos_sem_wait(&g_oss_task_sem, AOS_WAIT_FOREVER);
78 
79     JS_FreeCString(ctx, task_param.key);
80     JS_FreeCString(ctx, task_param.secret);
81     JS_FreeCString(ctx, task_param.endPoint);
82     JS_FreeCString(ctx, task_param.bucketName);
83     JS_FreeCString(ctx, task_param.objectName);
84     JS_FreeCString(ctx, task_param.filePath);
85 
86     aos_printf("oss upload finished! %s\n", oss_resp_url);
87 
88     return JS_NewString(ctx, oss_resp_url);
89 }
90 
native_oss_download_file(JSContext * ctx,JSValueConst this_val,int argc,JSValueConst * argv)91 static JSValue native_oss_download_file(JSContext *ctx, JSValueConst this_val,
92                           int argc, JSValueConst *argv)
93 {
94     int ret = -1;
95     char * url = NULL;
96     aos_task_t oss_task;
97     oss_task_param task_param;
98 
99     if (argc < 5)
100     {
101         amp_error(MOD_STR, "%s: args num is illegal :n_args = %d;\n", __func__, argc);
102         return JS_NewInt32(ctx, 0);
103     }
104 
105     task_param.flag = 1;
106     task_param.key = (char *)JS_ToCString(ctx, argv[0]);
107     task_param.secret = (char *)JS_ToCString(ctx, argv[1]);
108     task_param.endPoint = (char *)JS_ToCString(ctx, argv[2]);
109     task_param.bucketName = (char *)JS_ToCString(ctx, argv[3]);
110     task_param.objectName = (char *)JS_ToCString(ctx, argv[4]);
111     task_param.filePath = (char *)JS_ToCString(ctx, argv[5]);
112 
113     // amp_debug(MOD_STR, "key = %s;\n", task_param.key);
114     // amp_debug(MOD_STR, "secret = %s;\n", task_param.secret);
115     amp_debug(MOD_STR, "endPoint = %s;\n", task_param.endPoint);
116     amp_debug(MOD_STR, "bucketName = %s;\n", task_param.bucketName);
117     amp_debug(MOD_STR, "objectName = %s;\n", task_param.objectName);
118     amp_debug(MOD_STR, "filePath = %s;\n", task_param.filePath);
119 
120     aos_task_new_ext(&oss_task, "amp oss task", oss_task_handler, &task_param, 1024 * 10, AOS_DEFAULT_APP_PRI);
121     aos_sem_wait(&g_oss_task_sem, AOS_WAIT_FOREVER);
122 
123     JS_FreeCString(ctx, task_param.key);
124     JS_FreeCString(ctx, task_param.secret);
125     JS_FreeCString(ctx, task_param.endPoint);
126     JS_FreeCString(ctx, task_param.bucketName);
127     JS_FreeCString(ctx, task_param.objectName);
128     JS_FreeCString(ctx, task_param.filePath);
129 
130     aos_printf("oss download finished!\n");
131 
132     return JS_NewInt32(ctx, 0);
133 }
134 
135 
136 static JSClassDef js_oss_class = {
137     "oss",
138 };
139 
140 static const JSCFunctionListEntry js_oss_funcs[] = {
141     JS_CFUNC_DEF("uploadFile", 5, native_oss_upload_file ),
142     JS_CFUNC_DEF("downloadFile", 5, native_oss_download_file ),
143 };
144 
js_oss_init(JSContext * ctx,JSModuleDef * m)145 static int js_oss_init(JSContext *ctx, JSModuleDef *m)
146 {
147     JSValue proto;
148 
149     JS_NewClassID(&js_oss_class_id);
150 
151     JS_NewClass(JS_GetRuntime(ctx), js_oss_class_id, &js_oss_class);
152     proto = JS_NewObject(ctx);
153     JS_SetPropertyFunctionList(ctx, proto, js_oss_funcs,
154                                countof(js_oss_funcs));
155     JS_SetClassProto(ctx, js_oss_class_id, proto);
156 
157     return JS_SetModuleExportList(ctx, m, js_oss_funcs,
158                                   countof(js_oss_funcs));
159 }
160 
js_init_module_oss(JSContext * ctx,const char * module_name)161 JSModuleDef *js_init_module_oss(JSContext *ctx, const char *module_name)
162 {
163     JSModuleDef *m;
164     m = JS_NewCModule(ctx, module_name, js_oss_init);
165     if (!m)
166         return NULL;
167     JS_AddModuleExportList(ctx, m, js_oss_funcs, countof(js_oss_funcs));
168     return m;
169 }
170 
module_oss_register(void)171 void module_oss_register(void)
172 {
173     amp_debug(MOD_STR, "module_oss_register");
174     if (!g_oss_task_sem) {
175         if (aos_sem_new(&g_oss_task_sem, 0) != 0) {
176             amp_error(MOD_STR, "create amp oss sem fail");
177         }
178     }
179 
180     JSContext *ctx = js_get_context();
181 
182     js_init_module_oss(ctx, "oss");
183 }
184 
185 
186