📄 uploads_controller.php.svn-base
字号:
<?php/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Kubla CMS * * The Initial Developer of the Original Code is * The Mozilla Foundation. * Portions created by the Initial Developer are Copyright (C) 2006 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Wil Clouser <clouserw@mozilla.com> * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */vendor('BrowserAgent.class');class UploadsController extends AppController{ var $name = 'Uploads'; var $components = array('Joey', 'Pagination', 'Session', 'Storage');//@todo review these var $uses = array('Phone', 'Contentsource', 'Contentsourcetype', 'File', 'Upload','User'); var $helpers = array('Number','Time', 'Pagination'); // maybe move this to the storage component or into a table var $filetypes = array ( "videos" => array("video/3gpp", "video/flv", "video/mpeg", "video/avi", "video/quicktime"), "audio" => array("audio/x-wav", "audio/mpeg", "audio/mid"), "images" => array("image/png", "image/jpeg", "image/gif", "image/tiff", "image/bmp"), "rss" => array("rss-source/text"), "text" => array("text/plain"), "microsummaries" => array("microsummary/xml"), "widgets" => array("widget/joey"), ); /** * Set in beforeFilter(). Will hold the session user data. */ var $_user; /** * You can thank https://trac.cakephp.org/ticket/1589 for not letting us put this * in the constructor. (Apparently that is not a valid scenario...) */ function beforeFilter() { parent::beforeFilter(); // Set the local user variable to the Session's User $this->_user = $this->Session->read('User'); } /** * After add(), the UPLOADDIR has the following files: * random.orig.sfx: The original uploaded file. The .sfx indicates the file type. This file name and file type are saved in db ONLY IF there is no transcoded file for this upload. * random.png OR random.3gp: The transcoded file for image or video. The name and type are saved in the db. * previews/random.png: The preview. File name saved in db. */ function add() { $this->pageTitle = 'Add an upload'; $this->set('contentsourcetypes', $this->Contentsourcetype->generateList(null,null,null,'{n}.Contentsourcetype.id','{n}.Contentsourcetype.name')); // They've submitted data if (!empty($this->data)) { // Fill in the user_id FK. Cake needs this doubled up for the HABTM relationship $this->data['User']['User']['id'] = $this->_user['id']; // We've got two use cases. First, let's check if they've uploaded a // file successfully. if ( !empty($this->data['File']) && $this->data['File']['Upload']['error'] == 0 ) { if (!is_uploaded_file($this->data['File']['Upload']['tmp_name'])) { $this->File->invalidate('Upload'); $this->set('error_fileupload', 'Could not locate uploaded file.'); } // Check to see if the user has any additonal space. $filesize = filesize($this->data['File']['Upload']['tmp_name']); if ($this->Storage->hasAvailableSpace($this->_user['id'], $filesize) == false) { if ($this->nbClient) { $this->returnJoeyStatusCode($this->ERROR_NO_SPACE); } else { $_used = $this->Joey->bytesToReadableSize($this->User->totalSpaceUsedByUserId($this->_user['id'])); $_max = MAX_DISK_USAGE.' MB'; //this is already in MB $_size = $this->Joey->bytesToReadableSize($filesize); $this->set('error_fileupload', "You don't have enough space to save that file. You're using {$_used} out of {$_max} and your upload takes up {$_size}."); } $this->File->invalidate('Upload'); unlink($this->data['File']['Upload']['tmp_name']); } // Check if our form validates. This only checks the stuff in the // Upload and File models, (ie. are required fields filled in?) if ($this->Upload->validates($this->data) && $this->File->validates($this->data)) { // Get desired width and height for the transcoded media file $_phone = $this->Phone->findById($this->_user['phone_id']); $_width = intval ($_phone['Phone']['screen_width']); $_height = intval ($_phone['Phone']['screen_height']); // @todo fix data? if ($_width < 1 || $_height < 1) { // we have really no idea what the size should be, so lets just say $_width = 100; $_height = 100; } // Put our file away, generate the transcode file for mobile, as well as the preview. $_ret = $this->Storage->processUpload($this->data['File']['Upload']['tmp_name'], $this->_user['id'], $this->data['File']['Upload']['type'], $_width, $_height); if ($_ret == null) { $this->File->invalidate('Upload'); $this->set('error_fileupload', 'Could not move uploaded file.'); } $this->data['File']['name'] = basename($_ret['default_name']); $this->data['File']['type'] = $_ret['default_type']; $this->data['File']['size'] = filesize($_ret['default_name']); if (!empty($_ret['original_name'])) { $this->data['File']['original_name'] = basename($_ret['original_name']); $this->data['File']['original_type'] = $_ret['original_type']; $this->data['File']['original_size'] = filesize($_ret['original_name']); } if (!empty($_ret['preview_name'])) { $this->data['File']['preview_name'] = basename($_ret['preview_name']); $this->data['File']['preview_type'] = $_ret['preview_type']; $this->data['File']['preview_size'] = filesize($_ret['preview_name']); } // Start our transaction $this->Upload->begin(); if ($this->Upload->save($this->data)) { $this->data['File']['upload_id'] = $this->Upload->id; // This doesn't really matter anymore, but might as well whack it unset($this->data['File']['Upload']); if ($this->File->save($this->data)) { $this->Upload->setOwnerForUploadIdAndUserId($this->Upload->id, $this->_user['id']); $this->Upload->commit(); if ($this->nbClient) { $this->returnJoeyStatusCode($this->SUCCESS); } else { $this->flash('Upload saved.', '/uploads/index'); } } else { $this->Upload->rollback(); $this->set('error_mesg', 'Could not save your file.'); } } else { $this->Upload->rollback(); $this->set('error_mesg', 'Could not save your upload.'); } } // They uploaded a content source instead of a file } else if ( !empty($this->data['Contentsource']['source']) ) { $this->Upload->settings['throw_error'] = true; $this->Contentsource->settings['throw_error'] = true; // check for duplicates $_contentdup = $this->Contentsource->findBySource($this->data['Contentsource']['source']); if (!empty($_contentdup)) { if ($this->nbClient) { $this->returnJoeyStatusCode($this->ERROR_DUPLICATE); } else { $this->flash('Error - Duplicate found.', '/uploads/index'); } return; } // Remote clients don't know the ID's ahead of time, so they will // submit the type as a string. Here we look for that, and look up // the matching id. if (array_key_exists('name', $this->data['Contentsourcetype']) && !empty ($this->data['Contentsourcetype']['name'])) { $_contentsource = $this->Contentsourcetype->findByName($this->data['Contentsourcetype']['name'], array('id'), null, 0); // We found an ID that matched - Substitute that into the // request, and move on. If we don't find one that matches, // they're trying to submit a type that we currently don't // support. In that case, invalidate. if (array_key_exists('id', $_contentsource['Contentsourcetype']) && is_numeric($_contentsource['Contentsourcetype']['id'])) { $this->data['Contentsourcetype']['id'] = $_contentsource['Contentsourcetype']['id']; } else { $this->Contentsource->invalidate('name'); } } else if(!empty($this->data['Contentsourcetype']['id'])) { // As it turns out, we need the type as a string too $_contentsource = $this->Contentsourcetype->findById($this->data['Contentsourcetype']['id'], array('name'), null, 0); $this->data['Contentsourcetype']['name'] = $_contentsource['Contentsourcetype']['name']; } unset($_contentsource); if ($this->Upload->validates($this->data) && $this->Contentsource->validates($this->data) && $this->Contentsourcetype->validates($this->data)) { // Start our transaction. It doesn't matter what model we start // or end it on, all saves will be a part of it. $this->Upload->begin(); // This shouldn't ever fail, since we validated it if ($this->Upload->save($this->data)) { // Create a new file row if (($_file_id = $this->Storage->createFileForUploadId($this->Upload->id, $this->data['Contentsourcetype']['name'])) !== false) { // gg cake $this->data['Contentsource']['file_id'] = $_file_id; $this->data['Contentsource']['contentsourcetype_id'] = $this->data['Contentsourcetype']['id']; if ($this->Contentsource->save($this->data)) { $this->Upload->setOwnerForUploadIdAndUserId($this->Upload->id, $this->_user['id']); $this->Storage->updateFileByUploadId($this->Upload->id, true); $this->Upload->commit(); if ($this->nbClient) { $this->returnJoeyStatusCode($this->SUCCESS); } else { $this->flash('Upload saved.', '/uploads/index'); } } else { $this->Upload->rollback(); $this->set('error_mesg', 'There was an error saving your upload.'); } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -