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

orm: Move attachment chunk table to ORM

parent f665f9d3
No related branches found
No related tags found
No related merge requests found
...@@ -806,31 +806,52 @@ class FileStorageBackend { ...@@ -806,31 +806,52 @@ class FileStorageBackend {
* LOB fields in the MySQL database * LOB fields in the MySQL database
*/ */
define('CHUNK_SIZE', 500*1024); # Beware if you change this... define('CHUNK_SIZE', 500*1024); # Beware if you change this...
class AttachmentFileChunk extends VerySimpleModel {
static $meta = array(
'table' => FILE_CHUNK_TABLE,
'pk' => array('file_id', 'chunk_id'),
'joins' => array(
'file' => array(
'constraint' => array('file_id' => 'AttachmentFile.id'),
),
),
);
}
class AttachmentChunkedData extends FileStorageBackend { class AttachmentChunkedData extends FileStorageBackend {
static $desc = "In the database"; static $desc = /* @trans */ "In the database";
static $blocksize = CHUNK_SIZE; static $blocksize = CHUNK_SIZE;
function __construct($file) { function __construct($file) {
$this->file = $file; $this->file = $file;
$this->_chunk = 0; $this->_chunk = 0;
$this->_buffer = false; $this->_buffer = false;
$this->eof = false;
} }
function getSize() { function getSize() {
list($length) = db_fetch_row(db_query( $row = AttachmentFileChunk::objects()
'SELECT SUM(LENGTH(filedata)) FROM '.FILE_CHUNK_TABLE ->filter(array('file' => $this->file))
.' WHERE file_id='.db_input($this->file->getId()))); ->aggregate(array('length' => SqlAggregate::SUM(SqlFunction::LENGTH(new SqlField('filedata')))))
return $length; ->one();
return $row['length'];
} }
function read($amount=CHUNK_SIZE, $offset=0) { function read($amount=CHUNK_SIZE, $offset=0) {
# Read requested length of data from attachment chunks # Read requested length of data from attachment chunks
if ($this->eof)
return false;
while (strlen($this->_buffer) < $amount + $offset) { while (strlen($this->_buffer) < $amount + $offset) {
list($buf) = @db_fetch_row(db_query( try {
'SELECT filedata FROM '.FILE_CHUNK_TABLE.' WHERE file_id=' list($buf) = AttachmentFileChunk::objects()
.db_input($this->file->getId()).' AND chunk_id='.$this->_chunk++)); ->filter(array('file' => $this->file, 'chunk_id' => $this->_chunk++))
if (!$buf) ->values_flat('filedata')
->one();
}
catch (DoesNotExist $e) {
$this->eof = true;
break; break;
}
$this->_buffer .= $buf; $this->_buffer .= $buf;
} }
$chunk = substr($this->_buffer, $offset, $amount); $chunk = substr($this->_buffer, $offset, $amount);
...@@ -840,23 +861,27 @@ class AttachmentChunkedData extends FileStorageBackend { ...@@ -840,23 +861,27 @@ class AttachmentChunkedData extends FileStorageBackend {
function write($what, $chunk_size=CHUNK_SIZE) { function write($what, $chunk_size=CHUNK_SIZE) {
$offset=0; $offset=0;
for (;;) { while ($block = substr($what, $offset, $chunk_size)) {
$block = bin2hex(substr($what, $offset, $chunk_size)); // Chunks are considered immutable. Importing chunks should
if (!$block) break; // forceable remove the contents of a file before write()ing new
if (!db_query('REPLACE INTO '.FILE_CHUNK_TABLE // chunks. Therefore, inserts should be safe.
.' SET filedata=0x'.$block.', file_id=' $chunk = AttachmentFileChunk::create(array(
.db_input($this->file->getId()).', chunk_id='.db_input($this->_chunk++))) 'file' => $this->file,
'chunk_id' => $this->_chunk++,
'filedata' => $block
));
if (!$chunk->save())
return false; return false;
$offset += strlen($block)/2; $offset += strlen($block);
} }
return $this->_chunk; return $this->_chunk;
} }
function unlink() { function unlink() {
db_query('DELETE FROM '.FILE_CHUNK_TABLE return AttachmentFileChunk::objects()
.' WHERE file_id='.db_input($this->file->getId())); ->filter(array('file' => $this->file))
return db_affected_rows() > 0; ->delete();
} }
} }
FileStorageBackend::register('D', 'AttachmentChunkedData'); FileStorageBackend::register('D', 'AttachmentChunkedData');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment