Also use OC_Cache here.

This commit is contained in:
Thomas Tanghus 2012-06-06 00:22:03 +02:00
parent 80de23d08b
commit 624f8ae36d
1 changed files with 6 additions and 12 deletions

View File

@ -20,8 +20,6 @@
* *
*/ */
// Check if we are a user // 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::checkLoggedIn();
OCP\JSON::checkAppEnabled('contacts'); OCP\JSON::checkAppEnabled('contacts');
function bailOut($msg) { function bailOut($msg) {
@ -29,9 +27,6 @@ function bailOut($msg) {
OCP\Util::writeLog('contacts','ajax/oc_photo.php: '.$msg, OCP\Util::ERROR); OCP\Util::writeLog('contacts','ajax/oc_photo.php: '.$msg, OCP\Util::ERROR);
exit(); exit();
} }
function debug($msg) {
OCP\Util::writeLog('contacts','ajax/oc_photo.php: '.$msg, OCP\Util::DEBUG);
}
if(!isset($_GET['id'])) { if(!isset($_GET['id'])) {
bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.')); bailOut(OC_Contacts_App::$l10n->t('No contact ID was submitted.'));
@ -42,31 +37,30 @@ if(!isset($_GET['path'])) {
} }
$localpath = OC_Filesystem::getLocalFile($_GET['path']); $localpath = OC_Filesystem::getLocalFile($_GET['path']);
$tmpfname = tempnam(get_temp_dir(), "occOrig"); $tmpkey = 'contact-photo-'.$_GET['id'];
if(!file_exists($localpath)) { if(!file_exists($localpath)) {
bailOut(OC_Contacts_App::$l10n->t('File doesn\'t exist:').$localpath); bailOut(OC_Contacts_App::$l10n->t('File doesn\'t exist:').$localpath);
} }
file_put_contents($tmpfname, file_get_contents($localpath));
$image = new OC_Image(); $image = new OC_Image();
if(!$image) { if(!$image) {
bailOut(OC_Contacts_App::$l10n->t('Error loading image.')); bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
} }
if(!$image->loadFromFile($tmpfname)) { if(!$image->loadFromFile($localpath)) {
bailOut(OC_Contacts_App::$l10n->t('Error loading image.')); bailOut(OC_Contacts_App::$l10n->t('Error loading image.'));
} }
if($image->width() > 400 || $image->height() > 400) { if($image->width() > 400 || $image->height() > 400) {
$image->resize(400); // Prettier resizing than with browser and saves bandwidth. $image->resize(400); // Prettier resizing than with browser and saves bandwidth.
} }
if(!$image->fixOrientation()) { // No fatal error so we don't bail out. if(!$image->fixOrientation()) { // No fatal error so we don't bail out.
debug('Couldn\'t save correct image orientation: '.$tmpfname); OCP\Util::writeLog('contacts','ajax/oc_photo.php: Couldn\'t save correct image orientation: '.$localpath, OCP\Util::DEBUG);
} }
if($image->save($tmpfname)) { if(OC_Cache::set($tmpkey, $image->data(), 600)) {
OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpfname))); OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey)));
exit(); exit();
} else { } else {
bailOut('Couldn\'t save temporary image: '.$tmpfname); bailOut('Couldn\'t save temporary image: '.$tmpkey);
} }
?> ?>