Commit 8a3ffed9 authored by dopry's avatar dopry

Renamed stream to resource, added as a sub module of media.

Refactored ResourceStream classes.
parent dce6a5a3
; $Id$
name = Resource
description = A stream wrapper enabled replacement for core filehandling.
package = Media
core = 7.x
files[] = resource.module
<?php
// $Id$
function resource_install() {
drupal_install_schema('resource');
}
function resource_uninstall() {
drupal_uninstall_schema('resource');
}
function resource_enable() {
$path = file_directory_path();
if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) == FILE_DOWNLOADS_PUBLIC) {
$scheme = 'public:';
}
else {
$scheme = 'private:';
}
//db_query('INSERT INTO {file_backup} SELECT * FROM {files}');
//db_query('DELETE FROM {files}');
db_query("
INSERT INTO {resource} (rid, uid, name, url, mimetype, size, status, timestamp)
SELECT
fid, uid, filename, REPLACE(filepath, '%s', '%s'), filemime, filesize, status, timestamp
FROM {files}
", $path, $scheme);
}
function resource_disable() {
db_query('DELETE FROM {resource}');
//db_query('INSERT INTO {files} SELECT * FROM {file_backup}');
}
function resource_schema() {
$schema['resource'] = array(
'description' => t('Stores information for Resources.'),
'fields' => array(
'rid' => array(
'description' => t('Primary Key: Unique Resource ID.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'description' => t('The {users}.uid of the user who is associated with the resource.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'name' => array(
'description' => t('Name of the resource.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'url' => array(
'description' => t('URL of the resource.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'mimetype' => array(
'description' => t('The resource MIME type.'),
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'size' => array(
'description' => t('The size of the resource in bytes.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'status' => array(
'description' => t('A flag indicating whether file is temporary (0) or permanent (1).'),
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'timestamp' => array(
'description' => t('UNIX timestamp for when the file was added.'),
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'indexes' => array(
'uid' => array('uid'),
'status' => array('status'),
'timestamp' => array('timestamp'),
),
'primary key' => array('rid'),
);
return $schema;
}
This diff is collapsed.
<?php
// $Id$
/**
* @file
* This provides SimpleTests for the core stream wrapper functionality.
*/
/**
* Base class for file tests that adds some additional file specific
* assertions and helper functions.
*/
class ResourceStreamTestCase extends DrupalWebTestCase {
public $scheme = 'public://localhost/';
public $pathkey = 'stream_public_path';
public $pathdefault = '/sites/default/files';
public $filename = '';
public $dirname = '';
public $path = '';
public $handle = '';
public $streampath = '';
public $url = '';
function getInfo() {
return array(
'name' => t('ResourceStream Filesystem functions'),
'description' => t('Tests basic php stream functions.'),
'group' => t('Resource'),
);
}
function setUp() {
// create a known file and directory for testing.
resource_debug($this->scheme);
$this->realpath = realpath(variable_get($this->pathkey, $this->pathdefault));
$this->dirname = 'ResourceStreamTestCaseDir' . uniqid();
$this->filename = 'ResourceStreamTestCaseFile' . uniqid() . '.txt';
$this->path = '/'. $this->dirname . '/' . $this->filename;
$this->url = $this->scheme . $this->path;
resource_debug("realpath: $this->realpath");
$data = '1234567890abcdef';
mkdir($this->realpath .'/'. $this->dirname);
file_put_contents($this->realpath . $this->path, $data, LOCK_EX);
$this->assertTrue(file_exists($this->realpath . $this->path), 'Source file created.');
}
function tearDown() {
unlink($this->realpath . $this->path);
rmdir($this->realpath .'/'. $this->dirname);
}
/**
* fopen calls the stream_open method of a streamwrapper.
*/
function testFopen() {
$this->handle = fopen($this->url, 'r+');
$this->assertTrue($this->handle, 'fopen: '. $this->url);
}
// fread - stream_read
function testFread() {
$data = fread($this->handle, 4);
$this->assertTrue($data == '1234', 'fread 4 bytes, start pos 0');
$pos = ftell($this->handle);
$this->assertTrue($pos == 4, "ftell at pos 4($pos)");
$data = fread($this->handle, 4);
$this->assertTrue($data == '5678', 'fread 4 bytes, start pos 4');
// There is apparently a bug with either ftell or fseek working here.
// ftell should return the currently position. Seek is obviously
// moving the file pointer or the reads wouldn't match. Either fseek
// isn't updating the pos read by ftell, or ftell is actually returning
// bytes read.
$this->assertTrue(fseek($this->handle, 8), 'fseek to pos 0');
$pos = ftell($this->handle);
$this->assertTrue($pos == 8, "ftell at pos 8($pos)");
$data = fread($this->handle, 4);
$this->assertTrue($data == '90ab', "fread 4 bytes, start pos 4($data)");
$this->assertFalse(feof($this->handle), 'feof at pos 4');
$data = fgets($this->handle);
$this->assertTrue($data == 'cdef', 'fgets pos 12 - 15');
$this->assertTrue(feof($this->handle), 'feof at end of file');
$this->assertTrue(fseek($this->handle, 4), 'fseek to pos 4');
$this->assertTrue(fclose($this->handle), "Close file.");
}
// fwrite - stream_write
function testFwrite() {
$this->handle = fopen($this->scheme . $this->path, 'a+');
$this->assertTrue($this->handle, "Open $this->scheme$this->path in mode a+");
$data = '1234';
$this->assertTrue(fwrite($this->handle, $data), "fwrite ($data) to handle");
$this->assertTrue(fseek($this->handle, -4, SEEK_END), "Seek to pos before fwrite.");
$read = fread($this->handle, 4);
$this->assertTrue($data == $read, "fread ($read) == fwritten ($data) bytes");
$this->assertTrue(fclose($this->handle), "Close file.");
}
function testFilePutGetContents() {
$data = 'fedcba0987654321';
$this->assertTrue(file_put_contents($this->scheme . $this->path, $data), "file_put_contents($data)");
$read = file_get_contents($this->scheme . $this->path);
$this->assertTrue($data == $read, "file_get_contents ($read) == file_put_contents ($data) bytes");
}
function testCopyRenameUnlink() {
copy($this->scheme . $this->path, $this->scheme . $this->path .'.2');
$this->assertTrue(file_exists($this->scheme . $this->path .'.2'), "Copy successful");
unlink($this->scheme . $this->path);
$this->assertFalse(file_exists($this->scheme . $this->path), "Unlink successful");
rename($this->scheme . $this->path .'.2', $this->scheme . $this->path);
$this->assertTrue(file_exists($this->scheme . $this->path), "Rename successful");
}
/*
// fflush - stream_flush
function testFflush() {
}
// fstat - stream_stat
function testFstat() {
}
*/
/*
// php >= 5.3.0 Implement in PHP 5.3 is required.
// stream_select - stream_cast
// stream_set_blocking - stream_set_option
// stream_set_timeout - stream_set_option
// stream_set_write_buffer - stream_set_option
// mkdir - mkdir
function testMkDir() {
}
// rmdir - rmdir
function testRmDir() {
}
// opendir - dir_opendir
function testOpenDir() {
}
// stat - url_stat
function testStat() {
}
// readdir - dir_readdir
function testReadDir() {
}
// rewinddir - dir_rewinddir
function testRewindDir() {
}
// closedir - dir_closedir
function testCloseDir() {
}
*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment