3
3
4
4
use MongoDB \Collection ;
5
5
use MongoDB \Exception \RuntimeException ;
6
+ use MongoDB \Exception \UnexpectedTypeException ;
7
+ use MongoDB \Exception \InvalidArgumentException ;
6
8
use MongoDB \BSON \ObjectId ;
7
9
/**
8
10
* GridFsupload abstracts the processes of inserting into a GridFSBucket
12
14
class GridFsUpload extends GridFsStream
13
15
{
14
16
private $ ctx ;
17
+ private $ bufferLength ;
15
18
/**
16
19
* Constructs a GridFS upload stream
17
20
*
@@ -44,6 +47,7 @@ public function __construct(
44
47
array $ options =[]
45
48
)
46
49
{
50
+ $ this ->bufferLength = 0 ;
47
51
$ this ->ctx = hash_init ('md5 ' );
48
52
$ uploadDate = time ();
49
53
$ objectId = new \MongoDB \BSON \ObjectId ();
@@ -77,7 +81,6 @@ public function __construct(
77
81
throw new InvalidArgumentTypeException ('"metadata" option ' , $ options ['metadata ' ], 'object or array ' );
78
82
}
79
83
}
80
-
81
84
$ this ->file = array_merge ($ main_file , $ fileOptions );
82
85
parent ::__construct ($ filesCollection , $ chunksCollection , $ chunkSizeBytes );
83
86
}
@@ -89,6 +92,14 @@ public function __construct(
89
92
*/
90
93
public function uploadFromStream ($ source )
91
94
{
95
+ if (!is_resource ($ source ) || get_resource_type ($ source ) != "stream " ) {
96
+ throw new UnexpectedTypeException ('stream ' , $ source );
97
+ } else {
98
+ $ streamMetadata = stream_get_meta_data ($ source );
99
+ } if (!is_readable ($ streamMetadata ['uri ' ])) {
100
+ // throw new InvalidArgumentException("stream not readable");
101
+ //issue being that php's is_readable reports native streams as not readable like php://temp
102
+ }
92
103
while ($ data = fread ($ source , $ this ->chunkSizeBytes )) {
93
104
$ this ->insertChunk ($ data );
94
105
}
@@ -102,6 +113,20 @@ public function uploadFromStream($source)
102
113
*/
103
114
public function insertChunks ($ toWrite )
104
115
{
116
+ $ readBytes = 0 ;
117
+
118
+ while ($ readBytes != strlen ($ toWrite )) {
119
+ $ addToBuffer = substr ($ toWrite , $ readBytes , $ this ->chunkSizeBytes - $ this ->bufferLength );
120
+ fwrite ($ this ->buffer , $ addToBuffer );
121
+ $ readBytes += strlen ($ addToBuffer );
122
+ $ this ->bufferLength += strlen ($ addToBuffer );
123
+ if ($ this ->bufferLength == $ this ->chunkSizeBytes ) {
124
+ $ this ->insertChunk (fread ($ this ->buffer , $ this ->chunkSizeBytes ));
125
+ ftruncate ($ this ->buffer ,0 );
126
+ $ this ->bufferLength = 0 ;
127
+ }
128
+ }
129
+ /*
105
130
rewind($this->buffer);
106
131
$cached = fread($this->buffer, $this->chunkSizeBytes);
107
132
$toWrite = $cached . $toWrite;
@@ -117,7 +142,8 @@ public function insertChunks($toWrite)
117
142
}
118
143
$this->insertChunk($bytes[$i]);
119
144
}
120
- return strlen ($ toWrite );
145
+ */
146
+ return $ readBytes ;
121
147
}
122
148
/**
123
149
* Close an active stream, pushes all buffered data to GridFS
@@ -138,14 +164,6 @@ public function close()
138
164
139
165
$ this ->fileCollectionInsert ();
140
166
}
141
- /*
142
- private function insertChunk($data)
143
- {
144
- $toUpload = ["files_id" => $this->file['_id'], "n" => $this->n, "data" => $data];
145
- hash_update($this->ctx, $data);
146
- $this->chunksCollection->insertOne($toUpload);
147
- $this->n++;
148
- } */
149
167
private function insertChunk ($ data )
150
168
{
151
169
$ toUpload = ["files_id " => $ this ->file ['_id ' ], "n " => $ this ->n , "data " => $ data ];
0 commit comments