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 
25 class NET_API NET_HTTPUploadedFile
26 {
27 public:
29  {
30  SENTINEL
31  };
32 
36  myName(UT_StringRef::SENTINEL),
37  myContentType(UT_StringRef::SENTINEL),
38  mySize(0),
39  myInMemoryValue(),
40  myTempFileName(UT_StringRef::SENTINEL)
41  {
42  }
43 
45 
46  /// The name of the file found in the Content-Disposition header.
47  SYS_FORCE_INLINE const UT_StringHolder &name() const { return myName; }
48  /// The size of the file.
49  SYS_FORCE_INLINE int64 size() const { return mySize; }
50  /// The content type of the file.
52  {
53  return myContentType;
54  }
55  /// The temp file location. This is only filled out if isInMemory()
56  /// return false
57  const UT_StringHolder &fileLocation() const { return myTempFileName; }
58 
59  /// Is this file in memory or stored on disk
61  {
62  return !myTempFileName.isstring();
63  }
64 
65  void forceToStorage();
66 
67  /// Determine if the file needs to be read in multiple chunks. This is
68  /// useful to determining if you can perform read() safeully.
69  /// Default uses the servers max size of 2.5MB
70  bool multipleChunks(int64 chunk_size = -1) const;
71 
72  /// Remove the temp file. This does nothing if the file is in memory
73  void removeTempFile();
74 
76  void makeSentinel()
77  {
78  myName.makeSentinel();
79  myContentType.makeSentinel();
80  mySize = 0;
81  myInMemoryValue.clear();
82  myTempFileName.makeSentinel();
83  }
84 
86  bool isSentinel() const
87  {
88  // Name is required no matter what.
89  return myName.isSentinel();
90  }
91 
92  /// Read data from an uploaded file
93  class NET_API IStream
94  {
95  public:
97  ~IStream();
98 
99  /// Read the entire uploaded data from the file. Use chunks() if you
100  /// are not certain about the total file size.
101  bool read(UT_WorkBuffer &data);
102  bool read(std::string &data);
103 
104  /// Read in chunks of the file. It is often best to use this function
105  /// instead of read() to avoid overwhelming your system's memory.
106  int64 readNextChunk(int64 chunk_size, UT_WorkBuffer &data);
107  int64 readNextChunk(int64 chunk_size, std::string &data);
108 
109  void close();
110 
112 
113  private:
114  int64 myAmountRead;
115  UT_IFStream myStream;
116  };
117 
118 private:
119  friend class NET_HttpIO;
120 
121  void write(const char *data, int64 length);
122  void writeToStorage(const char* data, int64 length);
123 
124  void convertInMemoryToStorage();
125  /// Clear out this object to read again.
126  void clear();
127 
128  /// Parse the upload info from the header line
129  /// Content-Disposition: form-data; name=<name>; file<name>
130  void parseInfo(const UT_StringRef &header_value, UT_StringHolder &name);
131 
132  UT_StringHolder myName;
133 
134  UT_StringHolder myContentType;
135  int64 mySize;
136  UT_Array<char> myInMemoryValue;
137  UT_StringHolder myTempFileName;
138 };
139 
140 namespace UT
141 {
142 template <>
144 {
145  static void clear(NET_HTTPUploadedFile &ufile) { ufile.makeSentinel(); }
146  static bool isClear(NET_HTTPUploadedFile &ufile) { return ufile.isSentinel(); }
148  {
150  }
151  static const bool clearNeedsDestruction = false;
152 };
153 }
154 
155 #endif // __NET_HTTPUPLOADEDFILE_H__
156 
GLuint const GLchar * name
Definition: glew.h:1814
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()
void close() override
void read(T &in, bool &v)
Definition: ImfXdr.h:611
SYS_FORCE_INLINE const UT_StringHolder & contentType() const
The content type of the file.
const UT_StringHolder & fileLocation() const
SYS_FORCE_INLINE const UT_StringHolder & name() const
The name of the file found in the Content-Disposition header.
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
static void clearConstruct(NET_HTTPUploadedFile *ufile)
GLuint GLsizei GLsizei * length
Definition: glew.h:1825
long long int64
Definition: SYS_Types.h:116
GLsizei const GLchar *const * string
Definition: glew.h:1844
Read data from an uploaded file.
NET_HTTPUploadedFile(SentinelType)
void write(T &out, bool v)
Definition: ImfXdr.h:332
SYS_FORCE_INLINE int64 size() const
The size of the file.
SYS_FORCE_INLINE bool isSentinel() const