Skip to content
Snippets Groups Projects
Commit fda938c6 authored by Jared Hancock's avatar Jared Hancock
Browse files

Simplify interface to AttachmentChunkedData

parent bf957037
No related branches found
No related tags found
No related merge requests found
...@@ -230,7 +230,7 @@ class AttachmentFile { ...@@ -230,7 +230,7 @@ class AttachmentFile {
* in the FILE_CHUNK_TABLE to overcome the max_allowed_packet limitation of * in the FILE_CHUNK_TABLE to overcome the max_allowed_packet limitation of
* LOB fields in the MySQL database * LOB fields in the MySQL database
*/ */
define('CHUNK_SIZE', 256*1024); # Beware if you change this... define('CHUNK_SIZE', 500*1024); # Beware if you change this...
class AttachmentChunkedData { class AttachmentChunkedData {
function AttachmentChunkedData($file) { function AttachmentChunkedData($file) {
$this->_file = $file; $this->_file = $file;
...@@ -244,51 +244,24 @@ class AttachmentChunkedData { ...@@ -244,51 +244,24 @@ class AttachmentChunkedData {
return $length; return $length;
} }
function seek($location) {
$this->_pos=$location;
}
function tell() {
return $this->_pos;
}
function read($length=CHUNK_SIZE) { function read($length=CHUNK_SIZE) {
# Read requested length of data from attachment chunks # Read requested length of data from attachment chunks
$buffer=''; list($buffer) = @db_fetch_row(db_query(
while ($length > 0) { 'SELECT filedata FROM '.FILE_CHUNK_TABLE.' WHERE file_id='
$chunk_id = floor($this->_pos / CHUNK_SIZE); .db_input($this->_file).' AND chunk_id='.$this->_pos++));
$start = $this->_pos % CHUNK_SIZE;
$size = min($length, CHUNK_SIZE - $start);
list($block) = @db_fetch_row(db_query(
'SELECT SUBSTR(filedata, '.($start+1).', '.$size
.') FROM '.FILE_CHUNK_TABLE.' WHERE file_id='
.db_input($this->_file).' AND chunk_id='.$chunk_id));
if (!$block) return false;
$buffer .= $block;
$this->_pos += $size;
$length -= $size;
}
return $buffer; return $buffer;
} }
function write($what) { function write($what, $chunk_size=CHUNK_SIZE) {
# Figure out the remaining part of the current chunk (use CHUNK_SIZE
# and $this->_pos, increment pointer into $what and continue to end
# of what
$offset=0; $offset=0;
for (;;) { for (;;) {
$start = $this->_pos % CHUNK_SIZE; $block = substr($what, $offset, $chunk_size);
$size = CHUNK_SIZE - $start;
$block = substr($what, $offset, $size);
if (!$block) break; if (!$block) break;
if (!db_query('REPLACE INTO '.FILE_CHUNK_TABLE if (!db_query('REPLACE INTO '.FILE_CHUNK_TABLE
.' SET filedata=INSERT(filedata, '.($start+1).','.$size .' SET filedata=0x'.bin2hex($block).', file_id='
.', 0x'.bin2hex($block) .db_input($this->_file).', chunk_id='.db_input($this->_pos++)))
.'), file_id='.db_input($this->_file)
.', chunk_id='.floor($this->_pos / CHUNK_SIZE)))
return false; return false;
$offset += $size; $offset += strlen($block);
$this->_pos += strlen($block);
} }
return true; return true;
} }
......
...@@ -6,15 +6,6 @@ ...@@ -6,15 +6,6 @@
* *
*/ */
CREATE TABLE `%TABLE_PREFIX%T_file_chunk_id` ( `id` int(11) );
-- Support up to 16MB attachments
INSERT INTO `%TABLE_PREFIX%T_file_chunk_id` VALUES (0), (1), (2), (3), (4),
(5), (6), (7), (8), (9), (10), (11), (12), (13), (14), (15), (16), (17),
(18), (19), (20), (21), (22), (23), (24), (25), (26), (27), (28), (29),
(30), (31), (32), (33), (34), (35), (36), (37), (38), (39), (40), (41),
(42), (43), (44), (45), (46), (47), (48), (49), (50), (51), (52), (53),
(54), (55), (56), (57), (58), (59), (60), (61), (62), (63);
DROP TABLE IF EXISTS `%TABLE_PREFIX%file_chunk`; DROP TABLE IF EXISTS `%TABLE_PREFIX%file_chunk`;
CREATE TABLE `%TABLE_PREFIX%file_chunk` ( CREATE TABLE `%TABLE_PREFIX%file_chunk` (
`file_id` int(11) NOT NULL, `file_id` int(11) NOT NULL,
...@@ -24,16 +15,12 @@ CREATE TABLE `%TABLE_PREFIX%file_chunk` ( ...@@ -24,16 +15,12 @@ CREATE TABLE `%TABLE_PREFIX%file_chunk` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8; ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `%TABLE_PREFIX%file_chunk` (`file_id`, `chunk_id`, `filedata`) INSERT INTO `%TABLE_PREFIX%file_chunk` (`file_id`, `chunk_id`, `filedata`)
SELECT T1.`id`, T2.`id`, SELECT `id`, 0, `filedata`
SUBSTR(T1.`filedata`, T2.`id` * 256 * 1024 + 1, 256 * 1024) FROM `%TABLE_PREFIX%file`;
FROM `%TABLE_PREFIX%file` T1, `%TABLE_PREFIX%T_file_chunk_id` T2
WHERE T2.`id` * 256 * 1024 < LENGTH(T1.`filedata`);
ALTER TABLE `%TABLE_PREFIX%file` DROP COLUMN `filedata`; ALTER TABLE `%TABLE_PREFIX%file` DROP COLUMN `filedata`;
OPTIMIZE TABLE `%TABLE_PREFIX%file`; OPTIMIZE TABLE `%TABLE_PREFIX%file`;
DROP TABLE `%TABLE_PREFIX%T_file_chunk_id`;
-- Finished with patch -- Finished with patch
UPDATE `%TABLE_PREFIX%config` UPDATE `%TABLE_PREFIX%config`
SET `schema_signature`='dd0022fb14892c0bb6a9700392df2de7'; SET `schema_signature`='dd0022fb14892c0bb6a9700392df2de7';
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment