HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_HTTPUploadedFile.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_HTTPUploadedFile.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_HTTPUPLOADEDFILE_H__
13 #define __NET_HTTPUPLOADEDFILE_H__
14 
15 #include "NET_API.h"
16 
17 #include <UT/UT_Array.h>
18 #include <UT/UT_IStream.h>
19 #include <UT/UT_NonCopyable.h>
20 #include <UT/UT_OFStream.h>
21 #include <UT/UT_StringHolder.h>
22 #include <UT/UT_WorkBuffer.h>
23 
24 #include <SYS/SYS_Compiler.h>
25 
27 {
28 public:
29  static constexpr int64 DefaultMaxInMemorySize = 2500000; // 2.5MB
30 
32  {
33  SENTINEL
34  };
35 
39  myName(UT_StringRef::SENTINEL),
40  myContentType(UT_StringRef::SENTINEL),
41  mySize(0),
42  myInMemoryValue(),
43  myTempFileName(UT_StringRef::SENTINEL),
44  myMaxInMemorySize(DefaultMaxInMemorySize)
45  {
46  }
47 
49 
50  // Movable
52  NET_HTTPUploadedFile &operator=(NET_HTTPUploadedFile &&) = default;
53 
54  // But not copyable
56 
57  /// The name of the file found in the Content-Disposition header.
58  SYS_FORCE_INLINE const UT_StringHolder &name() const { return myName; }
59  /// The size of the file.
60  SYS_FORCE_INLINE int64 size() const { return mySize; }
61  /// The content type of the file.
63  {
64  return myContentType;
65  }
66  /// The temp file location. This is only filled out if isInMemory()
67  /// return false
68  const UT_StringHolder &fileLocation() const { return myTempFileName; }
69 
70  /// Is this file in memory or stored on disk
72  {
73  return !myTempFileName.isstring();
74  }
75 
76  void forceToStorage();
77 
78  /// Determine if the file needs to be read in multiple chunks. This is
79  /// useful to determining if you can perform read() safeully.
80  /// Default uses the servers max size of 2.5MB
81  bool multipleChunks(int64 chunk_size = -1) const;
82 
83  /// Remove the temp file. This does nothing if the file is in memory
84  void removeTempFile();
85 
87  void makeSentinel()
88  {
89  myName.makeSentinel();
90  myContentType.makeSentinel();
91  mySize = 0;
92  myInMemoryValue.clear();
93  myTempFileName.makeSentinel();
94  }
95 
97  bool isSentinel() const
98  {
99  // Name is required no matter what.
100  return myName.isSentinel();
101  }
102 
103  void setMaxInMemorySize(int64 max_size)
104  {
105  myMaxInMemorySize = max_size;
106  }
107 
108  /// Read data from an uploaded file
110  {
111  public:
113  ~IStream();
114 
115  /// Read the entire uploaded data from the file. Use chunks() if you
116  /// are not certain about the total file size.
117  bool read(UT_WorkBuffer &data);
118  bool read(std::string &data);
119 
120  /// Read in chunks of the file. It is often best to use this function
121  /// instead of read() to avoid overwhelming your system's memory.
122  int64 readNextChunk(int64 chunk_size, UT_WorkBuffer &data);
123  int64 readNextChunk(int64 chunk_size, std::string &data);
124 
125  void close();
126 
128 
129  private:
130  int64 myAmountRead;
131  UT_IFStream myStream;
132  };
133 
134 private:
135  friend class NET_HttpIO;
136 
137  void write(const char *data, int64 length);
138  void writeToStorage(const char* data, int64 length);
139 
140  void convertInMemoryToStorage();
141  /// Clear out this object to read again.
142  void clear();
143 
144  /// Parse the upload info from the header line
145  /// Content-Disposition: form-data; name=<name>; file<name>
146  void parseInfo(const UT_StringRef &header_value, UT_StringHolder &name);
147 
148  UT_StringHolder myName;
149 
150  UT_StringHolder myContentType;
151  int64 mySize;
152  UT_Array<char> myInMemoryValue;
153  UT_StringHolder myTempFileName;
154  int64 myMaxInMemorySize;
155 };
156 
157 namespace UT
158 {
159 template <>
161 {
162  static void clear(NET_HTTPUploadedFile &ufile) { ufile.makeSentinel(); }
163  static bool isClear(NET_HTTPUploadedFile &ufile) { return ufile.isSentinel(); }
165  {
167  }
168  static const bool clearNeedsDestruction = false;
169 };
170 }
171 
172 #endif // __NET_HTTPUPLOADEDFILE_H__
173 
SYS_FORCE_INLINE bool isInMemory() const
Is this file in memory or stored on disk.
static void clear(NET_HTTPUploadedFile &ufile)
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
static bool isClear(NET_HTTPUploadedFile &ufile)
SYS_FORCE_INLINE void makeSentinel()
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
void close() override
void read(T &in, bool &v)
Definition: ImfXdr.h:502
SYS_FORCE_INLINE const UT_StringHolder & contentType() const
The content type of the file.
#define NET_API
Definition: NET_API.h:9
const UT_StringHolder & fileLocation() const
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
static void clearConstruct(NET_HTTPUploadedFile *ufile)
void setMaxInMemorySize(int64 max_size)
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
long long int64
Definition: SYS_Types.h:116
GLuint const GLchar * name
Definition: glcorearb.h:786
Read data from an uploaded file.
NET_HTTPUploadedFile(SentinelType)
#define const
Definition: zconf.h:214
void write(T &out, bool v)
Definition: ImfXdr.h:287
SYS_FORCE_INLINE int64 size() const
The size of the file.
Definition: format.h:895
SYS_FORCE_INLINE bool isSentinel() const
class IMF_EXPORT_TYPE IStream
Definition: ImfForward.h:89