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_StringHolder.h>
19 #include <UT/UT_WorkBuffer.h>
20 #include <UT/UT_OFStream.h>
21 #include <UT/UT_IStream.h>
22 
23 #include <SYS/SYS_Compiler.h>
24 
26 {
27 public:
28  static constexpr int64 DefaultMaxInMemorySize = 2500000; // 2.5MB
29 
31  {
32  SENTINEL
33  };
34 
38  myName(UT_StringRef::SENTINEL),
39  myContentType(UT_StringRef::SENTINEL),
40  mySize(0),
41  myInMemoryValue(),
42  myTempFileName(UT_StringRef::SENTINEL),
43  myMaxInMemorySize(DefaultMaxInMemorySize)
44  {
45  }
46 
48 
49  /// The name of the file found in the Content-Disposition header.
50  SYS_FORCE_INLINE const UT_StringHolder &name() const { return myName; }
51  /// The size of the file.
52  SYS_FORCE_INLINE int64 size() const { return mySize; }
53  /// The content type of the file.
55  {
56  return myContentType;
57  }
58  /// The temp file location. This is only filled out if isInMemory()
59  /// return false
60  const UT_StringHolder &fileLocation() const { return myTempFileName; }
61 
62  /// Is this file in memory or stored on disk
64  {
65  return !myTempFileName.isstring();
66  }
67 
68  void forceToStorage();
69 
70  /// Determine if the file needs to be read in multiple chunks. This is
71  /// useful to determining if you can perform read() safeully.
72  /// Default uses the servers max size of 2.5MB
73  bool multipleChunks(int64 chunk_size = -1) const;
74 
75  /// Remove the temp file. This does nothing if the file is in memory
76  void removeTempFile();
77 
79  void makeSentinel()
80  {
81  myName.makeSentinel();
82  myContentType.makeSentinel();
83  mySize = 0;
84  myInMemoryValue.clear();
85  myTempFileName.makeSentinel();
86  }
87 
89  bool isSentinel() const
90  {
91  // Name is required no matter what.
92  return myName.isSentinel();
93  }
94 
95  void setMaxInMemorySize(int64 max_size)
96  {
97  myMaxInMemorySize = max_size;
98  }
99 
100  /// Read data from an uploaded file
102  {
103  public:
105  ~IStream();
106 
107  /// Read the entire uploaded data from the file. Use chunks() if you
108  /// are not certain about the total file size.
109  bool read(UT_WorkBuffer &data);
110  bool read(std::string &data);
111 
112  /// Read in chunks of the file. It is often best to use this function
113  /// instead of read() to avoid overwhelming your system's memory.
114  int64 readNextChunk(int64 chunk_size, UT_WorkBuffer &data);
115  int64 readNextChunk(int64 chunk_size, std::string &data);
116 
117  void close();
118 
120 
121  private:
122  int64 myAmountRead;
123  UT_IFStream myStream;
124  };
125 
126 private:
127  friend class NET_HttpIO;
128 
129  void write(const char *data, int64 length);
130  void writeToStorage(const char* data, int64 length);
131 
132  void convertInMemoryToStorage();
133  /// Clear out this object to read again.
134  void clear();
135 
136  /// Parse the upload info from the header line
137  /// Content-Disposition: form-data; name=<name>; file<name>
138  void parseInfo(const UT_StringRef &header_value, UT_StringHolder &name);
139 
140  UT_StringHolder myName;
141 
142  UT_StringHolder myContentType;
143  int64 mySize;
144  UT_Array<char> myInMemoryValue;
145  UT_StringHolder myTempFileName;
146  int64 myMaxInMemorySize;
147 };
148 
149 namespace UT
150 {
151 template <>
153 {
154  static void clear(NET_HTTPUploadedFile &ufile) { ufile.makeSentinel(); }
155  static bool isClear(NET_HTTPUploadedFile &ufile) { return ufile.isSentinel(); }
157  {
159  }
160  static const bool clearNeedsDestruction = false;
161 };
162 }
163 
164 #endif // __NET_HTTPUPLOADEDFILE_H__
165 
SYS_FORCE_INLINE bool isInMemory() const
Is this file in memory or stored on disk.
static void clear(NET_HTTPUploadedFile &ufile)
static bool isClear(NET_HTTPUploadedFile &ufile)
SYS_FORCE_INLINE void makeSentinel()
GLuint const GLchar * name
Definition: glcorearb.h:786
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
SYS_FORCE_INLINE const UT_StringHolder & name() const
The name of the file found in the Content-Disposition header.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
static void clearConstruct(NET_HTTPUploadedFile *ufile)
void setMaxInMemorySize(int64 max_size)
long long int64
Definition: SYS_Types.h:116
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
Read data from an uploaded file.
NET_HTTPUploadedFile(SentinelType)
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