wn_sample_upload.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * File : wn_sample_upload.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This software is dual-licensed: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation. For the terms of this
  9. * license, see <http://www.gnu.org/licenses/>.
  10. *
  11. * You are free to use this software under the terms of the GNU General
  12. * Public License, but WITHOUT ANY WARRANTY; without even the implied
  13. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. * See the GNU General Public License for more details.
  15. *
  16. * Alternatively for commercial application, you can contact us
  17. * by email <business@rt-thread.com> for commercial license.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2018-10-26 ChenYong First version
  22. */
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <rtthread.h>
  26. #if RT_VER_NUM >= 0x40100
  27. #include <fcntl.h> /* 当需要使用文件操作时,需要包含这个头文件 */
  28. #else
  29. #include <dfs_posix.h>
  30. #endif /*RT_VER_NUM >= 0x40100*/
  31. #include <webnet.h>
  32. #include <wn_module.h>
  33. #ifdef WEBNET_USING_UPLOAD
  34. /**
  35. * upload file.
  36. */
  37. static const char * sd_upload = "/webnet";
  38. static const char * upload_dir = "upload"; /* e.g: "upload" */
  39. static int file_size = 0;
  40. const char *get_file_name(struct webnet_session *session)
  41. {
  42. const char *path = RT_NULL, *path_last = RT_NULL;
  43. path_last = webnet_upload_get_filename(session);
  44. if (path_last == RT_NULL)
  45. {
  46. rt_kprintf("file name err!!\n");
  47. return RT_NULL;
  48. }
  49. path = strrchr(path_last, '\\');
  50. if (path != RT_NULL)
  51. {
  52. path++;
  53. path_last = path;
  54. }
  55. path = strrchr(path_last, '/');
  56. if (path != RT_NULL)
  57. {
  58. path++;
  59. path_last = path;
  60. }
  61. return path_last;
  62. }
  63. static int upload_open(struct webnet_session *session)
  64. {
  65. int fd;
  66. const char *file_name = RT_NULL;
  67. file_name = get_file_name(session);
  68. rt_kprintf("Upload FileName: %s\n", file_name);
  69. rt_kprintf("Content-Type : %s\n", webnet_upload_get_content_type(session));
  70. if (webnet_upload_get_filename(session) != RT_NULL)
  71. {
  72. int path_size;
  73. char * file_path;
  74. path_size = strlen(sd_upload) + strlen(upload_dir)
  75. + strlen(file_name);
  76. path_size += 4;
  77. file_path = (char *)rt_malloc(path_size);
  78. if(file_path == RT_NULL)
  79. {
  80. fd = -1;
  81. goto _exit;
  82. }
  83. rt_sprintf(file_path, "%s/%s/%s", sd_upload, upload_dir, file_name);
  84. rt_kprintf("save to: %s\r\n", file_path);
  85. fd = open(file_path, O_WRONLY | O_CREAT, 0);
  86. if (fd < 0)
  87. {
  88. webnet_session_close(session);
  89. rt_free(file_path);
  90. fd = -1;
  91. goto _exit;
  92. }
  93. }
  94. file_size = 0;
  95. _exit:
  96. return (int)fd;
  97. }
  98. static int upload_close(struct webnet_session* session)
  99. {
  100. int fd;
  101. fd = (int)webnet_upload_get_userdata(session);
  102. if (fd < 0) return 0;
  103. close(fd);
  104. rt_kprintf("Upload FileSize: %d\n", file_size);
  105. return 0;
  106. }
  107. static int upload_write(struct webnet_session* session, const void* data, rt_size_t length)
  108. {
  109. int fd;
  110. fd = (int)webnet_upload_get_userdata(session);
  111. if (fd < 0) return 0;
  112. rt_kprintf("write: length %d\n", length);
  113. write(fd, data, length);
  114. file_size += length;
  115. return length;
  116. }
  117. static int upload_done (struct webnet_session* session)
  118. {
  119. const char* mimetype;
  120. static const char* status = "<html><head><title>Upload OK </title>"
  121. "</head><body>Upload OK, file length = %d "
  122. "<br/><br/><a href=\"javascript:history.go(-1);\">"
  123. "Go back to root</a></body></html>\r\n";
  124. /* get mimetype */
  125. mimetype = mime_get_type(".html");
  126. /* set http header */
  127. session->request->result_code = 200;
  128. webnet_session_set_header(session, mimetype, 200, "Ok", rt_strlen(status));
  129. webnet_session_printf(session, status, file_size);
  130. return 0;
  131. }
  132. const struct webnet_module_upload_entry upload_entry_upload =
  133. {
  134. "/upload",
  135. upload_open,
  136. upload_close,
  137. upload_write,
  138. upload_done
  139. };
  140. #endif /* WEBNET_USING_UPLOAD */