ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

157 lines
4.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_HTTP_FILE_H
  9. #define CPPCMS_HTTP_FILE_H
  10. #include <cppcms/defs.h>
  11. #include <cppcms/cstdint.h>
  12. #include <booster/hold_ptr.h>
  13. #include <booster/nowide/fstream.h>
  14. #include <booster/noncopyable.h>
  15. #include <sstream>
  16. #include <fstream>
  17. namespace cppcms {
  18. /// \cond INTERNAL
  19. namespace impl { class multipart_parser; }
  20. /// \endcond
  21. namespace http {
  22. class request;
  23. ///
  24. /// \brief This class holds a uploaded file, it is generally fetched via widgets::file or via http::request::files
  25. ///
  26. /// It provides full information about uploaded data as it was send by browser and allows to read the file via
  27. /// std::istream seek-able interface or save to the file system
  28. ///
  29. /// Note: this class does not perform any validations, for checking the data use widgets::file that allows to perform
  30. /// numerous checks on the file data.
  31. ///
  32. class CPPCMS_API file : public booster::noncopyable {
  33. public:
  34. ///
  35. /// Get the name of the POST field (i.e. <input name="value" ...>)
  36. ///
  37. std::string name() const;
  38. ///
  39. /// Get the content-type of the file as it was sent by the browser.
  40. ///
  41. std::string mime() const;
  42. ///
  43. /// Returns true if content type defined
  44. ///
  45. /// \ver{v1_2}
  46. bool has_mime() const;
  47. ///
  48. /// Get the filename as it was sent by the browser.
  49. ///
  50. std::string filename() const;
  51. ///
  52. /// Get std::istream on the data, please note, you need to call data().seekg(0) when using this
  53. /// stream first time.
  54. ///
  55. std::istream &data();
  56. ///
  57. /// Get the size of the file.
  58. ///
  59. long long size();
  60. ///
  61. /// Specify the path to the output file, note if is_temporary is true
  62. /// than the file would be deleted on cppcms::http::file destruction,
  63. /// unless save_to is called, otherwise it would remain persistent
  64. ///
  65. /// \ver{v1_2}
  66. void output_file(std::string const &name,bool is_temporary = false);
  67. ///
  68. /// Make sure that file created by output_file member function is not removed in destructor
  69. ///
  70. /// \ver{v1_2}
  71. void make_permanent();
  72. ///
  73. /// Close the file if it is still open, if the file temporary it is deleted, the the
  74. /// file in memory its content is removed, data() would return non-usable stream
  75. ///
  76. /// Returns 0 in case of sucess and -1 in case of failure
  77. ///
  78. /// \ver{v1_2}
  79. int close();
  80. ///
  81. /// Save file to file named \a filename. Throws cppcms_error in case of failure.
  82. ///
  83. /// Notes:
  84. ///
  85. /// - this function maybe more efficient then just reading the stream and writing it to newly created file, as
  86. /// in case of big files, it would try to move it over the file system
  87. /// - Under Win32 \a filename should be UTF-8 string
  88. ///
  89. void save_to(std::string const &filename);
  90. /// \cond INTERNAL
  91. void name(std::string const &);
  92. void mime(std::string const &);
  93. void filename(std::string const &);
  94. std::ostream &write_data();
  95. file();
  96. ~file();
  97. /// \endcond
  98. ///
  99. /// Set the maximal size of file that would be stored in memory instead of file system
  100. ///
  101. /// \ver{v1_2}
  102. void set_memory_limit(size_t size);
  103. ///
  104. /// Set the temporary directory where uploaded files are created
  105. ///
  106. /// \ver{v1_2}
  107. void set_temporary_directory(std::string const &dir);
  108. private:
  109. std::string name_;
  110. std::string mime_;
  111. std::string filename_;
  112. size_t size_limit_;
  113. booster::nowide::fstream res1_;
  114. std::stringstream res2_;
  115. std::string res3_;
  116. std::string res4_;
  117. void save_by_copy(std::string const &file_name,std::istream &in);
  118. void copy_stream(std::istream &in,std::ostream &out);
  119. uint32_t removed_ : 1 ;
  120. uint32_t file_specified_ : 1;
  121. uint32_t file_temporary_: 1;
  122. uint32_t reserverd_ : 29;
  123. struct impl_data; // for future use
  124. booster::hold_ptr<impl_data> d;
  125. friend class request;
  126. };
  127. } } //::cppcms::http
  128. #endif