[Isocial-svn] SF.net SVN: isocial: [229] app
Status: Pre-Alpha
Brought to you by:
aguidrevitch
From: <agu...@us...> - 2008-06-01 15:39:56
|
Revision: 229 http://isocial.svn.sourceforge.net/isocial/?rev=229&view=rev Author: aguidrevitch Date: 2008-06-01 08:40:00 -0700 (Sun, 01 Jun 2008) Log Message: ----------- Independent File storage (on local disk) implemented Modified Paths: -------------- app/application_controller.php app/controllers/photoapp/album_controller.php app/installers/photoapp/album_installer.php app/installers/user_installer.php app/models/photoapp_album.php app/models/photoapp_photo.php app/models/user.php app/shared_model.php app/views/photoapp/album/view.tpl Added Paths: ----------- app/models/base_file.php app/models/stored_file.php Modified: app/application_controller.php =================================================================== --- app/application_controller.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/application_controller.php 2008-06-01 15:40:00 UTC (rev 229) @@ -20,8 +20,10 @@ var $_errors = array(); - var $app_models = array('User', 'Message'); - var $app_helpers = array('AutoComplete', 'City', 'Dialog', 'AccessField'); + var $app_models = array('User', 'Message', 'BaseFile', 'StoredFile'); + var $app_helpers = array('AutoComplete', 'City', 'Dialog', 'AccessField'); + var $uploaded_files = array(); + var $current_user; function __construct () { @@ -40,6 +42,7 @@ function beforeAction ( $method = '' ) { $this->_validateLoginStatus(); + $this->_processFiles(); } function _validateLoginStatus () { @@ -47,6 +50,47 @@ $this->redirectTo(array( 'controller' => 'user', 'action' => 'login' )); } } + + + function _processFiles() { + $uploaded = $this->Request->_getNormalizedFilesArray($this->params); + foreach ($uploaded as $fieldname => $key_or_value) { + if (is_array($key_or_value)) { + foreach ($key_or_value as $key => $value) { + if ($stored = $this->_processSingleFile($key_or_value[$key])) { + array_push($this->uploaded_files, $stored); + } + } + } else { + if ($stored = $this->_processSingleFile($uploaded[$fieldname])) { + array_push($this->uploaded_files, $stored); + } + } + } + } + + function _isValidUploadedFile($tmpfile) { + return $this->_isImage($tmpfile['tmp_name']); + } + + function _isImage ($filename) { + return getimagesize($filename); + } + + function _processSingleFile($tmpfile) { + if (empty($tmpfile['error']) && $tmpfile['size'] > 0) { + if (!$this->_isValidUploadedFile($tmpfile)) { + return; + } + $stored = $this->current_user->stored_file->build(); + $stored->setAttributes($tmpfile); + if ($stored->upload($tmpfile['name'], $tmpfile['type'], $tmpfile['size'], $tmpfile['tmp_name'])) { + if ($stored->save()) { + return $stored; + } + } + } + } function _getUserFromSession () { if (isset($this->session['user_id'])) { Modified: app/controllers/photoapp/album_controller.php =================================================================== --- app/controllers/photoapp/album_controller.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/controllers/photoapp/album_controller.php 2008-06-01 15:40:00 UTC (rev 229) @@ -146,9 +146,24 @@ } } + /* + function _isValidUploadedFile($tmpfile) { + return true; + } + */ + function add_photo () { - if (!empty($this->params['album']) && !empty($_FILES)){ - $this->_upload($this->params); + if (!empty($this->params['album'])){ + $this->album = $this->PhotoappAlbum->find($this->params['id']); + foreach ($this->uploaded_files as $file) { + unset($photo); + $photo = $this->album->photo->build(); + $photo->base_file_id = $file->id; + //$photo->stored_file->assign($file); + //$photo->stored_file->build(); + //$photo->stored_file->assign($file); + $photo->save(); + } } else if ($this->params['id']) { $this->album = $this->PhotoappAlbum->find($this->params['id']); return; @@ -198,42 +213,6 @@ } } - function _upload($params) { - //$this->debug($_FILES); - for ($i = 1 ; $i < 6 ; $i++) { - $field = 'photo' . $i; - - if ($_FILES['album']['error'][$field] == 0 ) { - // copy file; save photo - $photo_name = microtime(true) . ".jpeg"; - $user_path = AK_PUBLIC_DIR. "/images/uploaded/" . $this->current_user->getId(); - if(!file_exists($user_path)) { - `mkdir $user_path`; - `chmod 777 $user_path`; - } - $copy_to = $user_path . "/" . $photo_name;//$_FILES['album']['name'][$field]; - $copy_from = $_FILES['album']['tmp_name'][$field]; - if (move_uploaded_file($copy_from, $copy_to)) { - $album = $this->PhotoappAlbum->find($this->params['id']); - $photo = new PhotoappPhoto (); - $photo->img = $this->current_user->getId() . "@" . $photo_name; - $photo->album_id = $album->getId(); - $photo->save(); - - $album = $this->PhotoappAlbum->find($this->params['id']); - if( empty($album->img)) { - $album->img = $photo->img; - $photo->save(); - } - //$album->photos_count += 1; - $album->save(); - } - } - } // for - $this->redirectTo(array('action' => 'uploaded', 'id' => $this->params['id'])); - - } - function defaultUrlOptions($options) { return array('id' => $this->params['id']); } Modified: app/installers/photoapp/album_installer.php =================================================================== --- app/installers/photoapp/album_installer.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/installers/photoapp/album_installer.php 2008-06-01 15:40:00 UTC (rev 229) @@ -22,12 +22,13 @@ $this->execute( "CREATE TABLE `photoapp_photos` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, - `album_id` int(11) NOT NULL , + `album_id` int(11) NOT NULL, + `base_file_id` int(11) NOT NULL, `name` char(100) NOT NULL DEFAULT '', `caption` char(255) NOT NULL DEFAULT '', - `created_at` datetime NOT NULL , - `img` char(255) NOT NULL DEFAULT '' - + FOREIGN KEY (album_id) REFERENCES photoapp_albums(id), + FOREIGN KEY (base_file_id) REFERENCES base_files(id), + INDEX album_idx (album_id) ) ENGINE=InnoDB"); Modified: app/installers/user_installer.php =================================================================== --- app/installers/user_installer.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/installers/user_installer.php 2008-06-01 15:40:00 UTC (rev 229) @@ -14,6 +14,18 @@ `updated_at` datetime NOT NULL, `active` tinyint(1) NOT NULL DEFAULT '0' ) ENGINE=InnoDB"); + + $this->execute( + "CREATE TABLE `base_files` ( + `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, + `user_id` int(11), + `name` char(255) NOT NULL, + `type` char(40) NOT NULL, + `size` int(11) NOT NULL, + `created_at` datetime NOT NULL, + FOREIGN KEY (user_id) REFERENCES users(id), + INDEX user_idx (user_id) + ) ENGINE=InnoDB"); $this->execute( "CREATE TABLE `confirmations` ( @@ -197,14 +209,10 @@ `address` varchar(100), `website` text, `city_id` int(11), - `mobile_access` int(11) NOT NULL DEFAULT 0, `land_access` int(11) NOT NULL DEFAULT 0, `address_access` int(11) NOT NULL DEFAULT 0, `website_access` int(11) NOT NULL DEFAULT 0, - - `created_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, FOREIGN KEY (user_id) REFERENCES users(id), FOREIGN KEY (city_id) REFERENCES cities(id), INDEX user_idx (user_id) @@ -238,16 +246,12 @@ `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `user_id` int(11) NOT NULL, `marital_status_id` int(11) NOT NULL, - `interested_in_men` bool, `interested_in_women` bool, `looking_for_friendship` bool, `looking_for_dating` bool, `looking_for_relationship` bool, `looking_for_networking` bool, - - `created_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, FOREIGN KEY (user_id) REFERENCES users(id), FOREIGN KEY (marital_status_id) REFERENCES marital_statuses(id), INDEX user_idx (user_id) @@ -257,6 +261,7 @@ "CREATE TABLE `personal_profiles` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `user_id` int(11) NOT NULL, + `former_name` varchar(100), `activities` text, `interests` text, `music` text, @@ -265,17 +270,42 @@ `books` text, `quotes` text, `about` text, - `created_at` datetime NOT NULL, - `updated_at` datetime NOT NULL, FOREIGN KEY (user_id) REFERENCES users(id), INDEX user_idx (user_id) ) ENGINE=InnoDB"); + + $this->execute( + "CREATE TABLE `groups` ( + `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, + `name` varchar(255), + `type` + `city_id` int(11), + `count` int(11), + FOREIGN KEY (user_id) REFERENCES users(id), + FOREIGN KEY (city_id) REFERENCES cities(id), + INDEX user_idx (user_id) + ) ENGINE=InnoDB"); + + $this->execute( + "CREATE TABLE `group_profiles` ( + `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `group_id` int(11), + `from` datetime, + `to` datetime, + `position` char(100), + `concentration` char(100), + FOREIGN KEY (user_id) REFERENCES users(id), + INDEX user_idx (user_id) + ) ENGINE=InnoDB"); } function down_1() { $this->transactionComplete(); + $this->dropTable('group_profiles'); + $this->dropTable('groups'); $this->dropTable('personal_profiles'); $this->dropTable('marital_statuses'); $this->dropTable('relationship_profiles'); Added: app/models/base_file.php =================================================================== --- app/models/base_file.php (rev 0) +++ app/models/base_file.php 2008-06-01 15:40:00 UTC (rev 229) @@ -0,0 +1,21 @@ +<?php + +class BaseFile extends ActiveRecord +{ + var $table_name = 'base_files'; + var $belongs_to = array('User'); + + function upload () { + die("upload method is virtual"); + } + + function remove () { + die("remove method is virtual"); + } + + function url () { + die("url method is virtual"); + } +} + +?> Modified: app/models/photoapp_album.php =================================================================== --- app/models/photoapp_album.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/models/photoapp_album.php 2008-06-01 15:40:00 UTC (rev 229) @@ -2,7 +2,6 @@ class PhotoappAlbum extends ActiveRecord { - //var $has_one = array('group_member' => array('class_name' => 'GroupappMember','foreign_key' => 'group_id'), ); var $has_many = array('photos' => array('class_name' => 'PhotoappPhoto', 'foreign_key' => 'album_id')); function validateOnCreate () Modified: app/models/photoapp_photo.php =================================================================== --- app/models/photoapp_photo.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/models/photoapp_photo.php 2008-06-01 15:40:00 UTC (rev 229) @@ -2,10 +2,12 @@ class PhotoappPhoto extends ActiveRecord { - var $belongs_to = array('album' => array('class_name' => 'PhotoappAlbum', 'primary_key_name' => 'album_id') ); + var $belongs_to = array('album' => array('class_name' => 'PhotoappAlbum', 'primary_key_name' => 'album_id'), + 'stored_file' => array('class_name' => 'StoredFile', 'primary_key_name' => 'base_file_id')); + /* function beforeDestroy() { - + $albums_cover = $this->album->find('all', array('conditions' => array("img = '" . $this->img . "'"))); if ($albums_cover) foreach ($albums_cover as $album) { @@ -19,14 +21,9 @@ $file = AK_PUBLIC_DIR . "/images/uploaded/" . $path; `rm -f $file`; return parent::beforeDestroy(); + } - - function debug($str) { - echo "<pre>"; - print_r($str); - echo "</pre>"; - } - + */ } ?> \ No newline at end of file Added: app/models/stored_file.php =================================================================== --- app/models/stored_file.php (rev 0) +++ app/models/stored_file.php 2008-06-01 15:40:00 UTC (rev 229) @@ -0,0 +1,30 @@ +<?php + +class StoredFile extends BaseFile +{ + + function path () { + return "images" . DS . "uploaded" . DS . $this->user_id . DS; + } + + function upload ($name, $type, $size, $tmpname) { + $user_path = AK_PUBLIC_DIR . DS . $this->path(); + if(!file_exists($user_path)) { + mkdir($user_path, 0755, true); + } + $forbidden = preg_quote('\/:*?"<>', '/'); + $this->name = microtime(true) . "_" . preg_replace("/([\\x00-\\x1f{$forbidden}])/e", "_", $name); + return move_uploaded_file($tmpname, $user_path . DS . $this->name); + } + + function remove () { + unlink(AK_PUBLIC_DIR . DS . $this->path() . DS . $this->name); + } + + function url () { + return AK_ASSET_URL_PREFIX . "/" . $this->path() . "/" . $this->name; + } + +} + +?> Modified: app/models/user.php =================================================================== --- app/models/user.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/models/user.php 2008-06-01 15:40:00 UTC (rev 229) @@ -12,6 +12,8 @@ 'conditions' => 'is_recipient_del = 0'), 'im_profiles' => array('class_name' => 'ImProfile', 'foreign_key' => 'user_id'), + 'stored_files' => array('class_name' => 'StoredFile', + 'foreign_key' => 'user_id'), /* 'groupapp_groups' => array('class_name' => 'GroupappMember', */ /* 'foreign_key' => 'user_id'), */ ); Modified: app/shared_model.php =================================================================== --- app/shared_model.php 2008-05-29 17:22:03 UTC (rev 228) +++ app/shared_model.php 2008-06-01 15:40:00 UTC (rev 229) @@ -14,6 +14,11 @@ */ class ActiveRecord extends AkActiveRecord { + + function getInheritanceColumn() { + return false; + } + function collect(&$source_array, $key_index, $value_index) { $resulting_array = array(); @@ -111,4 +116,9 @@ } -?> +class MediaItem extends ActiveRecord { + var $table_name = 'media_items'; +} + + +?> \ No newline at end of file Modified: app/views/photoapp/album/view.tpl =================================================================== --- app/views/photoapp/album/view.tpl 2008-05-29 17:22:03 UTC (rev 228) +++ app/views/photoapp/album/view.tpl 2008-06-01 15:40:00 UTC (rev 229) @@ -17,7 +17,8 @@ {loop photos} <div class="photo_view"> <div class="img"> - <img src="/preview/index/100x100/<?= $photo->get('img')?>" /> + <ximg src="/preview/index/100x100/<?= $photo->stored_file->load()->url()?>" /> + <img src="<? $photo->stored_file->load(); echo $photo->stored_file->url() ?>" /> </div> <div class="links"> <ul class="actions_list"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |