From 95cc7f627ffb6322a6d3592f1c57cf3815f0dd3a Mon Sep 17 00:00:00 2001 From: William MARTIN Date: Tue, 31 May 2011 13:58:12 +0200 Subject: [PATCH 1/4] Process a md5 on the uploaded file, and display it in the per file view. - The uploader can check that the uploaded file is correct - The downloader can check his download too --- src/IDF/Migrations/17DownloadMD5.php | 55 +++++++++++++++++++++++ src/IDF/Upload.php | 7 +++ src/IDF/templates/idf/downloads/view.html | 2 + 3 files changed, 64 insertions(+) create mode 100644 src/IDF/Migrations/17DownloadMD5.php diff --git a/src/IDF/Migrations/17DownloadMD5.php b/src/IDF/Migrations/17DownloadMD5.php new file mode 100644 index 0000000..3e275b5 --- /dev/null +++ b/src/IDF/Migrations/17DownloadMD5.php @@ -0,0 +1,55 @@ +getSqlTable(); + $sql = array(); + $sql['PostgreSQL'] = 'ALTER TABLE '.$table.' ADD COLUMN "md5" TEXT DEFAULT \'\''; + $sql['MySQL'] = 'ALTER TABLE '.$table.' ADD COLUMN `md5` LONGTEXT DEFAULT \'\''; + $db = Pluf::db(); + $engine = Pluf::f('db_engine'); + if (!isset($sql[$engine])) { + throw new Exception('SQLite complex migration not supported.'); + } + $db->execute($sql[$engine]); +} + +function IDF_Migrations_12DownloadDesc_down($params=null) +{ + $table = Pluf::factory('IDF_Upload')->getSqlTable(); + $sql = array(); + $sql['PostgreSQL'] = 'ALTER TABLE '.$table.' DROP COLUMN "md5"'; + $sql['MySQL'] = 'ALTER TABLE '.$table.' DROP COLUMN `md5`'; + $db = Pluf::db(); + $engine = Pluf::f('db_engine'); + if (!isset($sql[$engine])) { + throw new Exception('SQLite complex migration not supported.'); + } + $db->execute($sql[$engine]); + +} diff --git a/src/IDF/Upload.php b/src/IDF/Upload.php index d4e6094..b4105a2 100644 --- a/src/IDF/Upload.php +++ b/src/IDF/Upload.php @@ -77,6 +77,12 @@ class IDF_Upload extends Pluf_Model 'default' => 0, 'verbose' => __('file size in bytes'), ), + 'md5' => + array( + 'type' => 'Pluf_DB_Field_Text', + 'blank' => true, + 'verbose' => __('MD5'), + ), 'submitter' => array( 'type' => 'Pluf_DB_Field_Foreignkey', @@ -144,6 +150,7 @@ class IDF_Upload extends Pluf_Model if ($this->id == '') { $this->creation_dtime = gmdate('Y-m-d H:i:s'); $this->modif_dtime = gmdate('Y-m-d H:i:s'); + $this->md5 = md5_file (Pluf::f('upload_path') . '/' . $this->get_project()->shortname . '/files/' . $this->file); } } diff --git a/src/IDF/templates/idf/downloads/view.html b/src/IDF/templates/idf/downloads/view.html index 792c0a1..478a0b9 100644 --- a/src/IDF/templates/idf/downloads/view.html +++ b/src/IDF/templates/idf/downloads/view.html @@ -5,6 +5,8 @@
{if $deprecated}

{blocktrans}Attention! This file is marked as deprecated, download it only if you are sure you need this specific version.{/blocktrans}

{/if} {$file} - {$file.filesize|size} +
+{trans 'md5:'} {$file.md5}
{if $file.changelog}

{trans 'Changes'}

From 4d5418a60177e19d030f1920d182b8a8ce62f07f Mon Sep 17 00:00:00 2001 From: William MARTIN Date: Tue, 31 May 2011 14:01:39 +0200 Subject: [PATCH 2/4] Fix migration methods name --- src/IDF/Migrations/17DownloadMD5.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/IDF/Migrations/17DownloadMD5.php b/src/IDF/Migrations/17DownloadMD5.php index 3e275b5..34e030f 100644 --- a/src/IDF/Migrations/17DownloadMD5.php +++ b/src/IDF/Migrations/17DownloadMD5.php @@ -25,7 +25,7 @@ * Add the md5 column for the download model. */ -function IDF_Migrations_12DownloadMD5_up($params=null) +function IDF_Migrations_17DownloadMD5_up($params=null) { $table = Pluf::factory('IDF_Upload')->getSqlTable(); $sql = array(); @@ -39,7 +39,7 @@ function IDF_Migrations_12DownloadMD5_up($params=null) $db->execute($sql[$engine]); } -function IDF_Migrations_12DownloadDesc_down($params=null) +function IDF_Migrations_17DownloadMD5_down($params=null) { $table = Pluf::factory('IDF_Upload')->getSqlTable(); $sql = array(); From 06c57f7da6d20dd5eab8b82639f8faf3e63fbebb Mon Sep 17 00:00:00 2001 From: William MARTIN Date: Tue, 31 May 2011 14:53:44 +0200 Subject: [PATCH 3/4] Set the SQL type to VARCHAR(32) Process the md5 of each file during the migrationUpdate all IDF_Upload object to process their md5 during the migration --- src/IDF/Migrations/17DownloadMD5.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/IDF/Migrations/17DownloadMD5.php b/src/IDF/Migrations/17DownloadMD5.php index 34e030f..3b86bc2 100644 --- a/src/IDF/Migrations/17DownloadMD5.php +++ b/src/IDF/Migrations/17DownloadMD5.php @@ -27,20 +27,29 @@ function IDF_Migrations_17DownloadMD5_up($params=null) { + // Add the row $table = Pluf::factory('IDF_Upload')->getSqlTable(); $sql = array(); - $sql['PostgreSQL'] = 'ALTER TABLE '.$table.' ADD COLUMN "md5" TEXT DEFAULT \'\''; - $sql['MySQL'] = 'ALTER TABLE '.$table.' ADD COLUMN `md5` LONGTEXT DEFAULT \'\''; + $sql['PostgreSQL'] = 'ALTER TABLE '.$table.' ADD COLUMN "md5" VARCHAR(32) DEFAULT \'\''; + $sql['MySQL'] = 'ALTER TABLE '.$table.' ADD COLUMN `md5` VARCHAR(32) DEFAULT \'\''; $db = Pluf::db(); $engine = Pluf::f('db_engine'); if (!isset($sql[$engine])) { throw new Exception('SQLite complex migration not supported.'); } $db->execute($sql[$engine]); + + // Process md5 of already uploaded file + $files = Pluf::factory('IDF_Upload')->getList(); + foreach ($files as $f) { + $f->md5 = md5_file (Pluf::f('upload_path') . '/' . $f->get_project()->shortname . '/files/' . $f->file); + $f->update(); + } } function IDF_Migrations_17DownloadMD5_down($params=null) { + // Remove the row $table = Pluf::factory('IDF_Upload')->getSqlTable(); $sql = array(); $sql['PostgreSQL'] = 'ALTER TABLE '.$table.' DROP COLUMN "md5"'; @@ -51,5 +60,4 @@ function IDF_Migrations_17DownloadMD5_down($params=null) throw new Exception('SQLite complex migration not supported.'); } $db->execute($sql[$engine]); - } From d7857c512699df2305eb4ce2c8299e0fdee3a1bd Mon Sep 17 00:00:00 2001 From: William MARTIN Date: Fri, 10 Jun 2011 09:25:17 +0200 Subject: [PATCH 4/4] Rename the migration script --- src/IDF/Migrations/{17DownloadMD5.php => 18DownloadMD5.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/IDF/Migrations/{17DownloadMD5.php => 18DownloadMD5.php} (95%) diff --git a/src/IDF/Migrations/17DownloadMD5.php b/src/IDF/Migrations/18DownloadMD5.php similarity index 95% rename from src/IDF/Migrations/17DownloadMD5.php rename to src/IDF/Migrations/18DownloadMD5.php index 3b86bc2..159e909 100644 --- a/src/IDF/Migrations/17DownloadMD5.php +++ b/src/IDF/Migrations/18DownloadMD5.php @@ -25,7 +25,7 @@ * Add the md5 column for the download model. */ -function IDF_Migrations_17DownloadMD5_up($params=null) +function IDF_Migrations_18DownloadMD5_up($params=null) { // Add the row $table = Pluf::factory('IDF_Upload')->getSqlTable(); @@ -47,7 +47,7 @@ function IDF_Migrations_17DownloadMD5_up($params=null) } } -function IDF_Migrations_17DownloadMD5_down($params=null) +function IDF_Migrations_18DownloadMD5_down($params=null) { // Remove the row $table = Pluf::factory('IDF_Upload')->getSqlTable();