wn_sample_upload.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <dfs_posix.h>
  27. #include <webnet.h>
  28. #include <wn_module.h>
  29. #ifdef WEBNET_USING_UPLOAD
  30. /**
  31. * upload file.
  32. */
  33. static const char * sd_upload = "/webnet";
  34. static const char * upload_dir = "upload"; /* e.g: "upload" */
  35. static int file_size = 0;
  36. const char *get_file_name(struct webnet_session *session)
  37. {
  38. const char *path = RT_NULL, *path_last = RT_NULL;
  39. path_last = webnet_upload_get_filename(session);
  40. if (path_last == RT_NULL)
  41. {
  42. rt_kprintf("file name err!!\n");
  43. return RT_NULL;
  44. }
  45. path = strrchr(path_last, '\\');
  46. if (path != RT_NULL)
  47. {
  48. path++;
  49. path_last = path;
  50. }
  51. path = strrchr(path_last, '/');
  52. if (path != RT_NULL)
  53. {
  54. path++;
  55. path_last = path;
  56. }
  57. return path_last;
  58. }
  59. static int upload_open(struct webnet_session *session)
  60. {
  61. int fd;
  62. const char *file_name = RT_NULL;
  63. file_name = get_file_name(session);
  64. rt_kprintf("Upload FileName: %s\n", file_name);
  65. rt_kprintf("Content-Type : %s\n", webnet_upload_get_content_type(session));
  66. if (webnet_upload_get_filename(session) != RT_NULL)
  67. {
  68. int path_size;
  69. char * file_path;
  70. path_size = strlen(sd_upload) + strlen(upload_dir)
  71. + strlen(file_name);
  72. path_size += 4;
  73. file_path = (char *)rt_malloc(path_size);
  74. if(file_path == RT_NULL)
  75. {
  76. fd = -1;
  77. goto _exit;
  78. }
  79. sprintf(file_path, "%s/%s/%s", sd_upload, upload_dir, file_name);
  80. rt_kprintf("save to: %s\r\n", file_path);
  81. fd = open(file_path, O_WRONLY | O_CREAT, 0);
  82. if (fd < 0)
  83. {
  84. webnet_session_close(session);
  85. rt_free(file_path);
  86. fd = -1;
  87. goto _exit;
  88. }
  89. }
  90. file_size = 0;
  91. _exit:
  92. return (int)fd;
  93. }
  94. static int upload_close(struct webnet_session* session)
  95. {
  96. int fd;
  97. fd = (int)webnet_upload_get_userdata(session);
  98. if (fd < 0) return 0;
  99. close(fd);
  100. rt_kprintf("Upload FileSize: %d\n", file_size);
  101. return 0;
  102. }
  103. static int upload_write(struct webnet_session* session, const void* data, rt_size_t length)
  104. {
  105. int fd;
  106. fd = (int)webnet_upload_get_userdata(session);
  107. if (fd < 0) return 0;
  108. rt_kprintf("write: length %d\n", length);
  109. write(fd, data, length);
  110. file_size += length;
  111. return length;
  112. }
  113. static int upload_done (struct webnet_session* session)
  114. {
  115. const char* mimetype;
  116. static const char* status = "<html><head><title>Upload OK </title>"
  117. "</head><body>Upload OK, file length = %d "
  118. "<br/><br/><a href=\"javascript:history.go(-1);\">"
  119. "Go back to root</a></body></html>\r\n";
  120. /* get mimetype */
  121. mimetype = mime_get_type(".html");
  122. /* set http header */
  123. session->request->result_code = 200;
  124. webnet_session_set_header(session, mimetype, 200, "Ok", rt_strlen(status));
  125. webnet_session_printf(session, status, file_size);
  126. return 0;
  127. }
  128. const struct webnet_module_upload_entry upload_entry_upload =
  129. {
  130. "/upload",
  131. upload_open,
  132. upload_close,
  133. upload_write,
  134. upload_done
  135. };
  136. #endif /* WEBNET_USING_UPLOAD */