API: new procedure 'removeAllFiles' and contract change for 'createFile'

This commit is contained in:
Frederic Guillot 2015-06-13 15:47:48 -04:00
parent 4161015023
commit 0b7435b882
12 changed files with 315 additions and 134 deletions

View File

@ -36,13 +36,18 @@ class File extends Base
return '';
}
public function createFile($project_id, $task_id, $filename, $is_image, $blob)
public function createFile($project_id, $task_id, $filename, $blob)
{
return $this->file->uploadContent($project_id, $task_id, $filename, $is_image, $blob);
return $this->file->uploadContent($project_id, $task_id, $filename, $blob);
}
public function removeFile($file_id)
{
return $this->file->remove($file_id);
}
public function removeAllFiles($task_id)
{
return $this->file->removeAll($task_id);
}
}

View File

@ -19,7 +19,7 @@ class File extends Base
{
$task = $this->getTask();
if ($this->request->isPost() && $this->file->uploadScreenshot($task['project_id'], $task['id'], $this->request->getValue('screenshot'))) {
if ($this->request->isPost() && $this->file->uploadScreenshot($task['project_id'], $task['id'], $this->request->getValue('screenshot')) !== false) {
$this->session->flash(t('Screenshot uploaded successfully.'));

View File

@ -49,7 +49,8 @@ class File extends Base
{
$file = $this->getbyId($file_id);
if (! empty($file) && @unlink(FILES_DIR.$file['path'])) {
if (! empty($file)) {
@unlink(FILES_DIR.$file['path']);
return $this->db->table(self::TABLE)->eq('id', $file_id)->remove();
}
@ -66,10 +67,13 @@ class File extends Base
public function removeAll($task_id)
{
$files = $this->getAll($task_id);
$results = array();
foreach ($files as $file) {
$this->remove($file['id']);
$results[] = $this->remove($file['id']);
}
return ! in_array(false, $results, true);
}
/**
@ -79,26 +83,58 @@ class File extends Base
* @param integer $task_id Task id
* @param string $name Filename
* @param string $path Path on the disk
* @param bool $is_image Image or not
* @param integer $size File size
* @return bool
* @return bool|integer
*/
public function create($task_id, $name, $path, $is_image, $size)
public function create($task_id, $name, $path, $size)
{
$this->container['dispatcher']->dispatch(
self::EVENT_CREATE,
new FileEvent(array('task_id' => $task_id, 'name' => $name))
);
return $this->db->table(self::TABLE)->save(array(
$result = $this->db->table(self::TABLE)->save(array(
'task_id' => $task_id,
'name' => substr($name, 0, 255),
'path' => $path,
'is_image' => $is_image ? '1' : '0',
'is_image' => $this->isImage($name) ? 1 : 0,
'size' => $size,
'user_id' => $this->userSession->getId() ?: 0,
'date' => time(),
));
if ($result) {
$this->container['dispatcher']->dispatch(
self::EVENT_CREATE,
new FileEvent(array('task_id' => $task_id, 'name' => $name))
);
return (int) $this->db->getConnection()->getLastId();
}
return false;
}
/**
* Get PicoDb query to get all files
*
* @access public
* @return \PicoDb\Table
*/
public function getQuery()
{
return $this->db
->table(self::TABLE)
->columns(
self::TABLE.'.id',
self::TABLE.'.name',
self::TABLE.'.path',
self::TABLE.'.is_image',
self::TABLE.'.task_id',
self::TABLE.'.date',
self::TABLE.'.user_id',
self::TABLE.'.size',
User::TABLE.'.username',
User::TABLE.'.name as user_name'
)
->join(User::TABLE, 'id', 'user_id')
->asc(self::TABLE.'.name');
}
/**
@ -110,24 +146,7 @@ class File extends Base
*/
public function getAll($task_id)
{
return $this->db
->table(self::TABLE)
->columns(
self::TABLE.'.id',
self::TABLE.'.name',
self::TABLE.'.path',
self::TABLE.'.is_image',
self::TABLE.'.task_id',
self::TABLE.'.date',
self::TABLE.'.user_id',
self::TABLE.'.size',
User::TABLE.'.username',
User::TABLE.'.name as user_name'
)
->join(User::TABLE, 'id', 'user_id')
->eq('task_id', $task_id)
->asc(self::TABLE.'.name')
->findAll();
return $this->getQuery()->eq('task_id', $task_id)->findAll();
}
/**
@ -139,25 +158,7 @@ class File extends Base
*/
public function getAllImages($task_id)
{
return $this->db
->table(self::TABLE)
->columns(
self::TABLE.'.id',
self::TABLE.'.name',
self::TABLE.'.path',
self::TABLE.'.is_image',
self::TABLE.'.task_id',
self::TABLE.'.date',
self::TABLE.'.user_id',
self::TABLE.'.size',
User::TABLE.'.username',
User::TABLE.'.name as user_name'
)
->join(User::TABLE, 'id', 'user_id')
->eq('task_id', $task_id)
->eq('is_image', 1)
->asc(self::TABLE.'.name')
->findAll();
return $this->getQuery()->eq('task_id', $task_id)->eq('is_image', 1)->findAll();
}
/**
@ -169,29 +170,11 @@ class File extends Base
*/
public function getAllDocuments($task_id)
{
return $this->db
->table(self::TABLE)
->columns(
self::TABLE.'.id',
self::TABLE.'.name',
self::TABLE.'.path',
self::TABLE.'.is_image',
self::TABLE.'.task_id',
self::TABLE.'.date',
self::TABLE.'.user_id',
self::TABLE.'.size',
User::TABLE.'.username',
User::TABLE.'.name as user_name'
)
->join(User::TABLE, 'id', 'user_id')
->eq('task_id', $task_id)
->eq('is_image', 0)
->asc(self::TABLE.'.name')
->findAll();
return $this->getQuery()->eq('task_id', $task_id)->eq('is_image', 0)->findAll();
}
/**
* Check if a filename is an image
* Check if a filename is an image (file types that can be shown as thumbnail)
*
* @access public
* @param string $filename Filename
@ -226,24 +209,6 @@ class File extends Base
return $project_id.DIRECTORY_SEPARATOR.$task_id.DIRECTORY_SEPARATOR.hash('sha1', $filename.time());
}
/**
* Check if the base directory is created correctly
*
* @access public
*/
public function setup()
{
if (! is_dir(FILES_DIR)) {
if (! mkdir(FILES_DIR, 0755, true)) {
die('Unable to create the upload directory: "'.FILES_DIR.'"');
}
}
if (! is_writable(FILES_DIR)) {
die('The directory "'.FILES_DIR.'" must be writeable by your webserver user');
}
}
/**
* Handle file upload
*
@ -255,8 +220,7 @@ class File extends Base
*/
public function upload($project_id, $task_id, $form_name)
{
$this->setup();
$result = array();
$results = array();
if (! empty($_FILES[$form_name])) {
@ -272,11 +236,10 @@ class File extends Base
if (@move_uploaded_file($uploaded_filename, FILES_DIR.$destination_filename)) {
$result[] = $this->create(
$results[] = $this->create(
$task_id,
$original_filename,
$destination_filename,
$this->isImage($original_filename),
$_FILES[$form_name]['size'][$key]
);
}
@ -284,7 +247,7 @@ class File extends Base
}
}
return count(array_unique($result)) === 1;
return ! in_array(false, $results, true);
}
/**
@ -294,7 +257,7 @@ class File extends Base
* @param integer $project_id Project id
* @param integer $task_id Task id
* @param string $blob Base64 encoded image
* @return bool
* @return bool|integer
*/
public function uploadScreenshot($project_id, $task_id, $blob)
{
@ -314,7 +277,6 @@ class File extends Base
$task_id,
$original_filename,
$destination_filename,
true,
strlen($data)
);
}
@ -326,11 +288,10 @@ class File extends Base
* @param integer $project_id Project id
* @param integer $task_id Task id
* @param string $filename Filename
* @param bool $is_image Is image file?
* @param string $blob Base64 encoded image
* @return bool
* @return bool|integer
*/
public function uploadContent($project_id, $task_id, $filename, $is_image, &$blob)
public function uploadContent($project_id, $task_id, $filename, $blob)
{
$data = base64_decode($blob);
@ -347,7 +308,6 @@ class File extends Base
$task_id,
$filename,
$destination_filename,
$is_image,
strlen($data)
);
}

View File

@ -38,3 +38,15 @@ if (! is_writable('data')) {
if (ini_get('arg_separator.output') === '&') {
ini_set('arg_separator.output', '&');
}
// Prepare folder for uploaded files
if (! is_dir(FILES_DIR)) {
if (! mkdir(FILES_DIR, 0755, true)) {
die('Unable to create the upload directory: "'.FILES_DIR.'"');
}
}
// Check permissions for files folder
if (! is_writable(FILES_DIR)) {
die('The directory "'.FILES_DIR.'" must be writeable by your webserver user');
}

View File

@ -21,6 +21,7 @@ if (file_exists('config.php')) {
}
require __DIR__.'/constants.php';
require __DIR__.'/check_setup.php';
$container = new Pimple\Container;
$container->register(new ServiceProvider\LoggingProvider);

43
composer.lock generated
View File

@ -599,23 +599,23 @@
},
{
"name": "swiftmailer/swiftmailer",
"version": "v5.4.0",
"version": "v5.4.1",
"source": {
"type": "git",
"url": "https://github.com/swiftmailer/swiftmailer.git",
"reference": "31454f258f10329ae7c48763eb898a75c39e0a9f"
"reference": "0697e6aa65c83edf97bb0f23d8763f94e3f11421"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/31454f258f10329ae7c48763eb898a75c39e0a9f",
"reference": "31454f258f10329ae7c48763eb898a75c39e0a9f",
"url": "https://api.github.com/repos/swiftmailer/swiftmailer/zipball/0697e6aa65c83edf97bb0f23d8763f94e3f11421",
"reference": "0697e6aa65c83edf97bb0f23d8763f94e3f11421",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"require-dev": {
"mockery/mockery": "~0.9.1"
"mockery/mockery": "~0.9.1,<0.9.4"
},
"type": "library",
"extra": {
@ -644,23 +644,24 @@
"description": "Swiftmailer, free feature-rich PHP mailer",
"homepage": "http://swiftmailer.org",
"keywords": [
"email",
"mail",
"mailer"
],
"time": "2015-03-14 06:06:39"
"time": "2015-06-06 14:19:39"
},
{
"name": "symfony/console",
"version": "v2.7.0",
"version": "v2.7.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/Console.git",
"reference": "7f0bec04961c61c961df0cb8c2ae88dbfd83f399"
"reference": "564398bc1f33faf92fc2ec86859983d30eb81806"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/Console/zipball/7f0bec04961c61c961df0cb8c2ae88dbfd83f399",
"reference": "7f0bec04961c61c961df0cb8c2ae88dbfd83f399",
"url": "https://api.github.com/repos/symfony/Console/zipball/564398bc1f33faf92fc2ec86859983d30eb81806",
"reference": "564398bc1f33faf92fc2ec86859983d30eb81806",
"shasum": ""
},
"require": {
@ -704,20 +705,20 @@
],
"description": "Symfony Console Component",
"homepage": "https://symfony.com",
"time": "2015-05-29 16:22:24"
"time": "2015-06-10 15:30:22"
},
{
"name": "symfony/event-dispatcher",
"version": "v2.7.0",
"version": "v2.7.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/EventDispatcher.git",
"reference": "687039686d0e923429ba6e958d0baa920cd5d458"
"reference": "be3c5ff8d503c46768aeb78ce6333051aa6f26d9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/687039686d0e923429ba6e958d0baa920cd5d458",
"reference": "687039686d0e923429ba6e958d0baa920cd5d458",
"url": "https://api.github.com/repos/symfony/EventDispatcher/zipball/be3c5ff8d503c46768aeb78ce6333051aa6f26d9",
"reference": "be3c5ff8d503c46768aeb78ce6333051aa6f26d9",
"shasum": ""
},
"require": {
@ -762,22 +763,22 @@
],
"description": "Symfony EventDispatcher Component",
"homepage": "https://symfony.com",
"time": "2015-05-02 15:21:08"
"time": "2015-06-08 09:37:21"
}
],
"packages-dev": [
{
"name": "symfony/stopwatch",
"version": "v2.7.0",
"version": "v2.7.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/Stopwatch.git",
"reference": "7702945bceddc0e1f744519abb8a2baeb94bd5ce"
"reference": "c653f1985f6c2b7dbffd04d48b9c0a96aaef814b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/Stopwatch/zipball/7702945bceddc0e1f744519abb8a2baeb94bd5ce",
"reference": "7702945bceddc0e1f744519abb8a2baeb94bd5ce",
"url": "https://api.github.com/repos/symfony/Stopwatch/zipball/c653f1985f6c2b7dbffd04d48b9c0a96aaef814b",
"reference": "c653f1985f6c2b7dbffd04d48b9c0a96aaef814b",
"shasum": ""
},
"require": {
@ -813,7 +814,7 @@
],
"description": "Symfony Stopwatch Component",
"homepage": "https://symfony.com",
"time": "2015-05-02 15:21:08"
"time": "2015-06-04 20:11:48"
}
],
"aliases": [],

View File

@ -3542,9 +3542,8 @@ Response example:
- **project_id** (integer, required)
- **task_id** (integer, required)
- **filename** (integer, required)
- **is_image** (boolean, required)
- **blob** File content encoded in base64 (string, required)
- Result on success: **true**
- Result on success: **file_id**
- Result on failure: **false**
- Note: **The maximum file size depends of your PHP configuration, this method should not be used to upload large files**
@ -3554,12 +3553,11 @@ Request example:
{
"jsonrpc": "2.0",
"method": "createFile",
"id": 1035045925,
"id": 94500810,
"params": [
1,
1,
"My file",
false,
"cGxhaW4gdGV4dCBmaWxl"
]
}
@ -3570,8 +3568,8 @@ Response example:
```json
{
"jsonrpc": "2.0",
"id": 1035045925,
"result": true
"id": 94500810,
"result": 1
}
```
@ -3720,3 +3718,34 @@ Response example:
"result": true
}
```
### removeAllFiles
- Purpose: **Remove all files associated to a task**
- Parameters:
- **task_id** (integer, required)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "removeAllFiles",
"id": 593312993,
"params": {
"task_id": 1
}
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 593312993,
"result": true
}
```

View File

@ -1,6 +1,5 @@
<?php
require __DIR__.'/app/check_setup.php';
require __DIR__.'/app/common.php';
use Core\Router;

View File

@ -5,7 +5,7 @@
</testsuite>
</testsuites>
<php>
<const name="API_URL" value="http://localhost:8000/jsonrpc.php" />
<const name="API_URL" value="http://127.0.0.1:8000/jsonrpc.php" />
<const name="API_KEY" value="19ffd9709d03ce50675c3a43d1c49c1ac207f4bc45f06c5b2701fbdf8929" />
<const name="DB_DRIVER" value="sqlite" />
<const name="DB_FILENAME" value="data/db.sqlite" />

View File

@ -937,7 +937,7 @@ class Api extends PHPUnit_Framework_TestCase
public function testCreateFile()
{
$this->assertTrue($this->client->createFile(1, 1, 'My file', false, base64_encode('plain text file')));
$this->assertEquals(1, $this->client->createFile(1, 1, 'My file', base64_encode('plain text file')));
}
public function testGetAllFiles()
@ -962,4 +962,19 @@ class Api extends PHPUnit_Framework_TestCase
$this->assertTrue($this->client->removeFile($file['id']));
$this->assertEmpty($this->client->getAllFiles(1));
}
public function testRemoveAllFiles()
{
$this->assertEquals(1, $this->client->createFile(1, 1, 'My file 1', base64_encode('plain text file')));
$this->assertEquals(2, $this->client->createFile(1, 1, 'My file 2', base64_encode('plain text file')));
$files = $this->client->getAllFiles(array('task_id' => 1));
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertTrue($this->client->removeAllFiles(array('task_id' => 1)));
$files = $this->client->getAllFiles(array('task_id' => 1));
$this->assertEmpty($files);
}
}

View File

@ -9,6 +9,36 @@ use Model\Project;
class FileTest extends Base
{
public function testCreation()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->create(1, 'test', '/tmp/foo', 10));
$file = $f->getById(1);
$this->assertNotEmpty($file);
$this->assertEquals('test', $file['name']);
$this->assertEquals('/tmp/foo', $file['path']);
$this->assertEquals(0, $file['is_image']);
$this->assertEquals(1, $file['task_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(10, $file['size']);
$this->assertEquals(2, $f->create(1, 'test2.png', '/tmp/foobar', 10));
$file = $f->getById(2);
$this->assertNotEmpty($file);
$this->assertEquals('test2.png', $file['name']);
$this->assertEquals('/tmp/foobar', $file['path']);
$this->assertEquals(1, $file['is_image']);
}
public function testCreationFileNameTooLong()
{
$p = new Project($this->container);
@ -18,8 +48,8 @@ class FileTest extends Base
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertTrue($f->create(1, 'test', '/tmp/foo', false, 10));
$this->assertTrue($f->create(1, str_repeat('a', 1000), '/tmp/foo', false, 10));
$this->assertNotFalse($f->create(1, 'test', '/tmp/foo', 10));
$this->assertNotFalse($f->create(1, str_repeat('a', 1000), '/tmp/foo', 10));
$files = $f->getAll(1);
$this->assertNotEmpty($files);
@ -28,4 +58,133 @@ class FileTest extends Base
$this->assertEquals(str_repeat('a', 255), $files[0]['name']);
$this->assertEquals('test', $files[1]['name']);
}
public function testIsImage()
{
$f = new File($this->container);
$this->assertTrue($f->isImage('test.png'));
$this->assertTrue($f->isImage('test.jpeg'));
$this->assertTrue($f->isImage('test.gif'));
$this->assertTrue($f->isImage('test.jpg'));
$this->assertTrue($f->isImage('test.JPG'));
$this->assertFalse($f->isImage('test.bmp'));
$this->assertFalse($f->isImage('test'));
$this->assertFalse($f->isImage('test.pdf'));
}
public function testGeneratePath()
{
$f = new File($this->container);
$this->assertStringStartsWith('12/34/', $f->generatePath(12, 34, 'test.png'));
$this->assertNotEquals($f->generatePath(12, 34, 'test1.png'), $f->generatePath(12, 34, 'test2.png'));
}
public function testUploadScreenshot()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->uploadScreenshot(1, 1, base64_encode('image data')));
$file = $f->getById(1);
$this->assertNotEmpty($file);
$this->assertStringStartsWith('Screenshot taken ', $file['name']);
$this->assertStringStartsWith('1/1/', $file['path']);
$this->assertEquals(1, $file['is_image']);
$this->assertEquals(1, $file['task_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(10, $file['size']);
}
public function testUploadFileContent()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->uploadContent(1, 1, 'my file.pdf', base64_encode('file data')));
$file = $f->getById(1);
$this->assertNotEmpty($file);
$this->assertEquals('my file.pdf', $file['name']);
$this->assertStringStartsWith('1/1/', $file['path']);
$this->assertEquals(0, $file['is_image']);
$this->assertEquals(1, $file['task_id']);
$this->assertEquals(time(), $file['date'], '', 2);
$this->assertEquals(0, $file['user_id']);
$this->assertEquals(9, $file['size']);
}
public function testGetAll()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->create(1, 'B.pdf', '/tmp/foo', 10));
$this->assertEquals(2, $f->create(1, 'A.png', '/tmp/foo', 10));
$this->assertEquals(3, $f->create(1, 'D.doc', '/tmp/foo', 10));
$this->assertEquals(4, $f->create(1, 'C.JPG', '/tmp/foo', 10));
$files = $f->getAll(1);
$this->assertNotEmpty($files);
$this->assertCount(4, $files);
$this->assertEquals('A.png', $files[0]['name']);
$this->assertEquals('B.pdf', $files[1]['name']);
$this->assertEquals('C.JPG', $files[2]['name']);
$this->assertEquals('D.doc', $files[3]['name']);
$files = $f->getAllImages(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals('A.png', $files[0]['name']);
$this->assertEquals('C.JPG', $files[1]['name']);
$files = $f->getAllDocuments(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals('B.pdf', $files[0]['name']);
$this->assertEquals('D.doc', $files[1]['name']);
}
public function testRemove()
{
$p = new Project($this->container);
$f = new File($this->container);
$tc = new TaskCreation($this->container);
$this->assertEquals(1, $p->create(array('name' => 'test')));
$this->assertEquals(1, $tc->create(array('project_id' => 1, 'title' => 'test')));
$this->assertEquals(1, $f->create(1, 'B.pdf', '/tmp/foo', 10));
$this->assertEquals(2, $f->create(1, 'A.png', '/tmp/foo', 10));
$this->assertEquals(3, $f->create(1, 'D.doc', '/tmp/foo', 10));
$this->assertTrue($f->remove(2));
$files = $f->getAll(1);
$this->assertNotEmpty($files);
$this->assertCount(2, $files);
$this->assertEquals('B.pdf', $files[0]['name']);
$this->assertEquals('D.doc', $files[1]['name']);
$this->assertTrue($f->removeAll(1));
$files = $f->getAll(1);
$this->assertEmpty($files);
}
}

View File

@ -247,7 +247,7 @@ class NotificationTest extends Base
$this->assertEquals(1, $tc->create(array('title' => 'test', 'project_id' => 1)));
$this->assertEquals(1, $s->create(array('title' => 'test', 'task_id' => 1)));
$this->assertEquals(1, $c->create(array('comment' => 'test', 'task_id' => 1, 'user_id' => 1)));
$this->assertEquals(1, $f->create(1, 'test', 'blah', false, 123));
$this->assertEquals(1, $f->create(1, 'test', 'blah', 123));
$task = $tf->getDetails(1);
$subtask = $s->getById(1, true);