Contacts: Use OC_Cache for contact photo handling instead of temp dir.

This commit is contained in:
Thomas Tanghus 2012-06-05 20:30:22 +02:00
parent 098beae751
commit cb941996c0
7 changed files with 90 additions and 126 deletions

View File

@ -27,11 +27,11 @@
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('contacts');
$tmp_path = $_GET['tmp_path'];
$tmpkey = $_GET['tmpkey'];
$id = $_GET['id'];
OCP\Util::writeLog('contacts','ajax/cropphoto.php: tmp_path: '.$tmp_path.', exists: '.file_exists($tmp_path), OCP\Util::DEBUG);
//OCP\Util::writeLog('contacts','ajax/cropphoto.php: tmpkey: '.$tmpkey.', exists: '.file_exists($tmp_path), OCP\Util::DEBUG);
$tmpl = new OCP\Template("contacts", "part.cropphoto");
$tmpl->assign('tmp_path', $tmp_path);
$tmpl->assign('tmpkey', $tmpkey);
$tmpl->assign('id', $id);
$page = $tmpl->fetchPage();

View File

@ -19,10 +19,7 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Init owncloud
//require_once('../../../lib/base.php');
// Check if we are a user
// Firefox and Konqueror tries to download application/json for me. --Arthur
OCP\JSON::setContentTypeHeader('text/plain');
OCP\JSON::checkLoggedIn();
@ -32,30 +29,24 @@ function bailOut($msg) {
OCP\Util::writeLog('contacts','ajax/currentphoto.php: '.$msg, OCP\Util::ERROR);
exit();
}
function debug($msg) {
OCP\Util::writeLog('contacts','ajax/currentphoto.php: '.$msg, OCP\Util::DEBUG);
}
if (!isset($_GET['id'])) {
bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
}
$tmpfname = tempnam(get_temp_dir(), "occOrig");
$contact = OC_Contacts_App::getContactVCard($_GET['id']);
$image = new OC_Image();
if(!$image) {
bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
}
// invalid vcard
if( is_null($contact)) {
bailOut(OC_Contacts_App::$l10n->t('Error reading contact photo.'));
} else {
$image = new OC_Image();
if(!$image->loadFromBase64($contact->getAsString('PHOTO'))) {
$image->loadFromBase64($contact->getAsString('LOGO'));
}
if($image->valid()) {
if($image->save($tmpfname)) {
OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpfname)));
$tmpkey = 'contact-photo-'.md5($contact->getAsString('FN'));
if(OC_Cache::set($tmpkey, $image->data(), 600)) {
OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey)));
exit();
} else {
bailOut(OC_Contacts_App::$l10n->t('Error saving temporary file.'));

View File

@ -18,16 +18,11 @@
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* TODO: Translatable strings.
*/
// Check if we are a user
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('contacts');
// foreach ($_POST as $key=>$element) {
// OCP\Util::writeLog('contacts','ajax/savecrop.php: '.$key.'=>'.$element, OCP\Util::DEBUG);
// }
// Firefox and Konqueror tries to download application/json for me. --Arthur
OCP\JSON::setContentTypeHeader('text/plain');
@ -45,88 +40,71 @@ $y1 = (isset($_POST['y1']) && $_POST['y1']) ? $_POST['y1'] : 0;
//$y2 = isset($_POST['y2']) ? $_POST['y2'] : -1;
$w = (isset($_POST['w']) && $_POST['w']) ? $_POST['w'] : -1;
$h = (isset($_POST['h']) && $_POST['h']) ? $_POST['h'] : -1;
$tmp_path = isset($_POST['tmp_path']) ? $_POST['tmp_path'] : '';
$tmpkey = isset($_POST['tmpkey']) ? $_POST['tmpkey'] : '';
$id = isset($_POST['id']) ? $_POST['id'] : '';
if($tmp_path == '') {
bailOut('Missing path to temporary file.');
if($tmpkey == '') {
bailOut('Missing key to temporary file.');
}
if($id == '') {
bailOut('Missing contact id.');
}
OCP\Util::writeLog('contacts','savecrop.php: files: '.$tmp_path.' exists: '.file_exists($tmp_path), OCP\Util::DEBUG);
OCP\Util::writeLog('contacts','savecrop.php: key: '.$tmpkey, OCP\Util::DEBUG);
if(file_exists($tmp_path)) {
$data = OC_Cache::get($tmpkey);
if($data) {
$image = new OC_Image();
if($image->loadFromFile($tmp_path)) {
if($image->loadFromdata($data)) {
$w = ($w != -1 ? $w : $image->width());
$h = ($h != -1 ? $h : $image->height());
OCP\Util::writeLog('contacts','savecrop.php, x: '.$x1.' y: '.$y1.' w: '.$w.' h: '.$h, OCP\Util::DEBUG);
if($image->crop($x1, $y1, $w, $h)) {
if(($image->width() <= 200 && $image->height() <= 200) || $image->resize(200)) {
$tmpfname = tempnam(get_temp_dir(), "occCropped"); // create a new file because of caching issues.
if($image->save($tmpfname)) {
unlink($tmp_path);
$card = OC_Contacts_App::getContactVCard($id);
if(!$card) {
unlink($tmpfname);
bailOut('Error getting contact object.');
}
if($card->__isset('PHOTO')) {
OCP\Util::writeLog('contacts','savecrop.php: PHOTO property exists.', OCP\Util::DEBUG);
$property = $card->__get('PHOTO');
if(!$property) {
unlink($tmpfname);
bailOut('Error getting PHOTO property.');
}
$property->setValue($image->__toString());
$property->parameters[] = new Sabre_VObject_Parameter('ENCODING', 'b');
$property->parameters[] = new Sabre_VObject_Parameter('TYPE', $image->mimeType());
$card->__set('PHOTO', $property);
} else {
OCP\Util::writeLog('contacts','savecrop.php: files: Adding PHOTO property.', OCP\Util::DEBUG);
$card->addProperty('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $image->mimeType()));
}
$now = new DateTime;
$card->setString('REV', $now->format(DateTime::W3C));
if(!OC_Contacts_VCard::edit($id,$card)) {
bailOut('Error saving contact.');
}
unlink($tmpfname);
//$result=array( "status" => "success", 'mime'=>$image->mimeType(), 'tmp'=>$tmp_path);
$tmpl = new OCP\Template("contacts", "part.contactphoto");
$tmpl->assign('tmp_path', $tmpfname);
$tmpl->assign('mime', $image->mimeType());
$tmpl->assign('id', $id);
$tmpl->assign('refresh', true);
$tmpl->assign('width', $image->width());
$tmpl->assign('height', $image->height());
$page = $tmpl->fetchPage();
OCP\JSON::success(array('data' => array('page'=>$page, 'tmp'=>$tmpfname)));
exit();
} else {
if(file_exists($tmpfname)) {
unlink($tmpfname);
}
bailOut('Error saving temporary image');
$card = OC_Contacts_App::getContactVCard($id);
if(!$card) {
OC_Cache::remove($tmpkey);
bailOut(OC_Contacts_App::$l10n->t('Error getting contact object.'));
}
if($card->__isset('PHOTO')) {
OCP\Util::writeLog('contacts','savecrop.php: PHOTO property exists.', OCP\Util::DEBUG);
$property = $card->__get('PHOTO');
if(!$property) {
OC_Cache::remove($tmpkey);
bailOut(OC_Contacts_App::$l10n->t('Error getting PHOTO property.'));
}
$property->setValue($image->__toString());
$property->parameters[] = new Sabre_VObject_Parameter('ENCODING', 'b');
$property->parameters[] = new Sabre_VObject_Parameter('TYPE', $image->mimeType());
$card->__set('PHOTO', $property);
} else {
OCP\Util::writeLog('contacts','savecrop.php: files: Adding PHOTO property.', OCP\Util::DEBUG);
$card->addProperty('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $image->mimeType()));
}
$now = new DateTime;
$card->setString('REV', $now->format(DateTime::W3C));
if(!OC_Contacts_VCard::edit($id,$card)) {
bailOut(OC_Contacts_App::$l10n->t('Error saving contact.'));
}
$tmpl = new OCP\Template("contacts", "part.contactphoto");
$tmpl->assign('id', $id);
$tmpl->assign('refresh', true);
$tmpl->assign('width', $image->width());
$tmpl->assign('height', $image->height());
$page = $tmpl->fetchPage();
OCP\JSON::success(array('data' => array('page'=>$page)));
} else {
bailOut('Error resizing image');
bailOut(OC_Contacts_App::$l10n->t('Error resizing image'));
}
} else {
bailOut('Error cropping image');
bailOut(OC_Contacts_App::$l10n->t('Error cropping image'));
}
} else {
bailOut('Error creating temporary image');
bailOut(OC_Contacts_App::$l10n->t('Error creating temporary image'));
}
} else {
bailOut('Error finding image: '.$tmp_path);
bailOut(OC_Contacts_App::$l10n->t('Error finding image: ').$tmpkey);
}
if($tmp_path != '' && file_exists($tmp_path)) {
unlink($tmp_path);
}
?>
OC_Cache::remove($tmpkey);

View File

@ -19,14 +19,12 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Init owncloud
// Check if we are a user
// Firefox and Konqueror tries to download application/json for me. --Arthur
OCP\JSON::setContentTypeHeader('text/plain');
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('contacts');
// Firefox and Konqueror tries to download application/json for me. --Arthur
OCP\JSON::setContentTypeHeader('text/plain');
function bailOut($msg) {
OCP\JSON::error(array('data' => array('message' => $msg)));
OCP\Util::writeLog('contacts','ajax/uploadphoto.php: '.$msg, OCP\Util::DEBUG);
@ -39,46 +37,40 @@ function debug($msg) {
// If it is a Drag'n'Drop transfer it's handled here.
$fn = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : false);
if ($fn) {
// AJAX call
if (!isset($_GET['id'])) {
OCP\Util::writeLog('contacts','ajax/uploadphoto.php: No contact ID was submitted.', OCP\Util::DEBUG);
OCP\JSON::error(array('data' => array( 'message' => 'No contact ID was submitted.' )));
exit();
bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
}
$id = $_GET['id'];
$tmpfname = tempnam(get_temp_dir(), 'occOrig');
file_put_contents($tmpfname, file_get_contents('php://input'));
debug($tmpfname.' uploaded');
$tmpkey = 'contact-photo-'.md5($fn);
$data = file_get_contents('php://input');
$image = new OC_Image();
if($image->loadFromFile($tmpfname)) {
sleep(1); // Apparently it needs time to load the data.
if($image->loadFromData($data)) {
if($image->width() > 400 || $image->height() > 400) {
$image->resize(400); // Prettier resizing than with browser and saves bandwidth.
}
if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
debug('Couldn\'t save correct image orientation: '.$tmpfname);
debug('Couldn\'t save correct image orientation: '.$tmpkey);
}
if($image->save($tmpfname)) {
OCP\JSON::success(array('data' => array('mime'=>$_SERVER['CONTENT_TYPE'], 'name'=>$fn, 'id'=>$id, 'tmp'=>$tmpfname)));
if(OC_Cache::set($tmpkey, $image->data(), 600)) {
OCP\JSON::success(array('data' => array('mime'=>$_SERVER['CONTENT_TYPE'], 'name'=>$fn, 'id'=>$id, 'tmp'=>$tmpkey)));
exit();
} else {
bailOut('Couldn\'t save temporary image: '.$tmpfname);
bailOut(OC_Contacts_App::$l10n->t('Couldn\'t save temporary image: ').$tmpkey);
}
} else {
bailOut('Couldn\'t load temporary image: '.$file['tmp_name']);
bailOut(OC_Contacts_App::$l10n->t('Couldn\'t load temporary image: ').$tmpkey.$data);
}
}
// Uploads from file dialog are handled here.
if (!isset($_POST['id'])) {
OCP\Util::writeLog('contacts','ajax/uploadphoto.php: No contact ID was submitted.', OCP\Util::DEBUG);
OCP\JSON::error(array('data' => array( 'message' => 'No contact ID was submitted.' )));
exit();
bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
}
if (!isset($_FILES['imagefile'])) {
OCP\Util::writeLog('contacts','ajax/uploadphoto.php: No file was uploaded. Unknown error.', OCP\Util::DEBUG);
OCP\JSON::error(array('data' => array( 'message' => 'No file was uploaded. Unknown error' )));
exit();
bailOut(OC_Contacts_App::$l10n->t('No file was uploaded. Unknown error'));
}
$error = $_FILES['imagefile']['error'];
if($error !== UPLOAD_ERR_OK) {
$errors = array(
@ -93,27 +85,26 @@ if($error !== UPLOAD_ERR_OK) {
}
$file=$_FILES['imagefile'];
$tmpfname = tempnam(get_temp_dir(), "occOrig");
if(file_exists($file['tmp_name'])) {
$tmpkey = 'contact-photo-'.md5(basename($file['tmp_name']));
$image = new OC_Image();
if($image->loadFromFile($file['tmp_name'])) {
if($image->width() > 400 || $image->height() > 400) {
$image->resize(400); // Prettier resizing than with browser and saves bandwidth.
}
if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
debug('Couldn\'t save correct image orientation: '.$tmpfname);
debug('Couldn\'t save correct image orientation: '.$tmpkey);
}
if($image->save($tmpfname)) {
OCP\JSON::success(array('data' => array('mime'=>$file['type'],'size'=>$file['size'],'name'=>$file['name'], 'id'=>$_POST['id'], 'tmp'=>$tmpfname)));
if(OC_Cache::set($tmpkey, $image->data(), 600)) {
OCP\JSON::success(array('data' => array('mime'=>$file['type'],'size'=>$file['size'],'name'=>$file['name'], 'id'=>$_POST['id'], 'tmp'=>$tmpkey)));
exit();
} else {
bailOut('Couldn\'t save temporary image: '.$tmpfname);
bailOut(OC_Contacts_App::$l10n->t('Couldn\'t save temporary image: ').$tmpkey);
}
} else {
bailOut('Couldn\'t load temporary image: '.$file['tmp_name']);
bailOut(OC_Contacts_App::$l10n->t('Couldn\'t load temporary image: ').$file['tmp_name']);
}
} else {
bailOut('Temporary file: \''.$file['tmp_name'].'\' has gone AWOL?');
}
?>

View File

@ -1162,9 +1162,9 @@ Contacts={
}
});
},
editPhoto:function(id, tmp_path){
//alert('editPhoto: ' + tmp_path);
$.getJSON(OC.filePath('contacts', 'ajax', 'cropphoto.php'),{'tmp_path':tmp_path,'id':this.id},function(jsondata){
editPhoto:function(id, tmpkey){
//alert('editPhoto: ' + tmpkey);
$.getJSON(OC.filePath('contacts', 'ajax', 'cropphoto.php'),{'tmpkey':tmpkey,'id':this.id},function(jsondata){
if(jsondata.status == 'success'){
//alert(jsondata.data.page);
$('#edit_photo_dialog_img').html(jsondata.data.page);
@ -1194,6 +1194,7 @@ Contacts={
Contacts.UI.Card.loadPhotoHandlers();
}else{
OC.dialogs.alert(response.data.message, t('contacts', 'Error'));
wrapper.removeClass('wait');
}
});
Contacts.UI.Contacts.refreshThumbnail(this.id);

View File

@ -1,7 +1,7 @@
<?php
$id = $_['id'];
$tmp_path = $_['tmp_path'];
OCP\Util::writeLog('contacts','templates/part.cropphoto.php: tmp_path: '.$tmp_path.', exists: '.file_exists($tmp_path), OCP\Util::DEBUG);
$tmpkey = $_['tmpkey'];
OCP\Util::writeLog('contacts','templates/part.cropphoto.php: tmpkey: '.$tmpkey, OCP\Util::DEBUG);
?>
<script language="Javascript">
jQuery(function($) {
@ -38,7 +38,8 @@ OCP\Util::writeLog('contacts','templates/part.cropphoto.php: tmp_path: '.$tmp_pa
return true;
});*/
</script>
<img id="cropbox" src="<?php echo OCP\Util::linkToAbsolute('contacts', 'dynphoto.php'); ?>?tmp_path=<?php echo urlencode($tmp_path); ?>" />
<?php if(OC_Cache::hasKey($tmpkey)) { ?>
<img id="cropbox" src="<?php echo OCP\Util::linkToAbsolute('contacts', 'tmpphoto.php'); ?>?tmpkey=<?php echo $tmpkey; ?>" />
<form id="cropform"
class="coords"
method="post"
@ -47,7 +48,7 @@ OCP\Util::writeLog('contacts','templates/part.cropphoto.php: tmp_path: '.$tmp_pa
action="<?php echo OCP\Util::linkToAbsolute('contacts', 'ajax/savecrop.php'); ?>">
<input type="hidden" id="id" name="id" value="<?php echo $id; ?>" />
<input type="hidden" id="tmp_path" name="tmp_path" value="<?php echo $tmp_path; ?>" />
<input type="hidden" id="tmpkey" name="tmpkey" value="<?php echo $tmpkey; ?>" />
<fieldset id="coords">
<input type="hidden" id="x1" name="x1" value="" />
<input type="hidden" id="y1" name="y1" value="" />
@ -58,5 +59,8 @@ OCP\Util::writeLog('contacts','templates/part.cropphoto.php: tmp_path: '.$tmp_pa
</fieldset>
<iframe name="crop_target" id='crop_target' src=""></iframe>
</form>
<?php
} else {
echo $l->t('The temporary image has been removed from cache.');
}
?>

View File

@ -20,15 +20,14 @@
*
*/
// Init owncloud
$tmp_path = $_GET['tmp_path'];
$tmpkey = $_GET['tmpkey'];
$maxsize = isset($_GET['maxsize']) ? $_GET['maxsize'] : -1;
header("Cache-Control: no-cache, no-store, must-revalidate");
OCP\Util::writeLog('contacts','dynphoto.php: tmp_path: '.$tmp_path.', exists: '.file_exists($tmp_path), OCP\Util::DEBUG);
OCP\Util::writeLog('contacts','tmpphoto.php: tmpkey: '.$tmpkey, OCP\Util::DEBUG);
$image = new OC_Image($tmp_path);
$image = new OC_Image();
$image->loadFromData(OC_Cache::get($tmpkey));
if($maxsize != -1) {
$image->resize($maxsize);
}