mirror of
https://github.com/owncloudarchive/contacts.git
synced 2025-01-18 07:52:21 +01:00
Contacts: Improved image upload
This commit is contained in:
parent
a308a3ed21
commit
3c1ee05d53
@ -20,22 +20,39 @@
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Contacts;
|
||||
|
||||
// Firefox and Konqueror tries to download application/json for me. --Arthur
|
||||
OCP\JSON::setContentTypeHeader('text/plain');
|
||||
OCP\JSON::checkLoggedIn();
|
||||
OCP\JSON::checkAppEnabled('contacts');
|
||||
\OCP\JSON::setContentTypeHeader('text/plain');
|
||||
\OCP\JSON::checkLoggedIn();
|
||||
\OCP\JSON::checkAppEnabled('contacts');
|
||||
require_once 'loghandler.php';
|
||||
|
||||
if (!isset($_GET['id'])) {
|
||||
bailOut(OCA\Contacts\App::$l10n->t('No contact ID was submitted.'));
|
||||
$contactid = isset($_GET['contactid']) ? $_GET['contactid'] : '';
|
||||
$addressbookid = isset($_GET['addressbookid']) ? $_GET['addressbookid'] : '';
|
||||
$backend = isset($_GET['backend']) ? $_GET['backend'] : '';
|
||||
|
||||
if(!$contactid) {
|
||||
bailOut('Missing contact id.');
|
||||
}
|
||||
|
||||
$contact = OCA\Contacts\App::getContactVCard($_GET['id']);
|
||||
if(!$addressbookid) {
|
||||
bailOut('Missing address book id.');
|
||||
}
|
||||
|
||||
$app = new App();
|
||||
// FIXME: Get backend and addressbookid
|
||||
$contact = $app->getContact($backend, $addressbookid, $contactid);
|
||||
if(!$contact) {
|
||||
\OC_Cache::remove($tmpkey);
|
||||
bailOut(App::$l10n
|
||||
->t('Error getting contact object.'));
|
||||
}
|
||||
// invalid vcard
|
||||
if( is_null($contact)) {
|
||||
bailOut(OCA\Contacts\App::$l10n->t('Error reading contact photo.'));
|
||||
if(!$contact) {
|
||||
bailOut(App::$l10n->t('Error reading contact photo.'));
|
||||
} else {
|
||||
$image = new OC_Image();
|
||||
$image = new \OC_Image();
|
||||
if(!isset($contact->PHOTO) || !$image->loadFromBase64((string)$contact->PHOTO)) {
|
||||
if(isset($contact->LOGO)) {
|
||||
$image->loadFromBase64((string)$contact->LOGO);
|
||||
@ -43,13 +60,13 @@ if( is_null($contact)) {
|
||||
}
|
||||
if($image->valid()) {
|
||||
$tmpkey = 'contact-photo-'.$contact->UID;
|
||||
if(OC_Cache::set($tmpkey, $image->data(), 600)) {
|
||||
OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey)));
|
||||
if(\OC_Cache::set($tmpkey, $image->data(), 600)) {
|
||||
\OCP\JSON::success(array('data' => array('id'=>$_GET['id'], 'tmp'=>$tmpkey)));
|
||||
exit();
|
||||
} else {
|
||||
bailOut(OCA\Contacts\App::$l10n->t('Error saving temporary file.'));
|
||||
bailOut(App::$l10n->t('Error saving temporary file.'));
|
||||
}
|
||||
} else {
|
||||
bailOut(OCA\Contacts\App::$l10n->t('The loading photo is not valid.'));
|
||||
bailOut(App::$l10n->t('The loading photo is not valid.'));
|
||||
}
|
||||
}
|
||||
|
@ -47,10 +47,14 @@ if($tmpkey == '') {
|
||||
bailOut('Missing key to temporary file.');
|
||||
}
|
||||
|
||||
if($id == '') {
|
||||
if($contactid == '') {
|
||||
bailOut('Missing contact id.');
|
||||
}
|
||||
|
||||
if($addressbookid == '') {
|
||||
bailOut('Missing address book id.');
|
||||
}
|
||||
|
||||
\OCP\Util::writeLog('contacts', 'savecrop.php: key: '.$tmpkey, \OCP\Util::DEBUG);
|
||||
|
||||
$app = new App();
|
||||
@ -75,6 +79,12 @@ if($data) {
|
||||
if(($image->width() <= 200 && $image->height() <= 200)
|
||||
|| $image->resize(200)) {
|
||||
|
||||
// For vCard 3.0 the type must be e.g. JPEG or PNG
|
||||
// For version 4.0 the full mimetype should be used.
|
||||
// https://tools.ietf.org/html/rfc2426#section-3.1.4
|
||||
$type = strval($contact->VERSION) === '4.0'
|
||||
? $image->mimeType()
|
||||
: strtoupper(array_pop(explode('/', $image->mimeType())));
|
||||
if(isset($contact->PHOTO)) {
|
||||
\OCP\Util::writeLog('contacts',
|
||||
'savecrop.php: PHOTO property exists.',
|
||||
@ -86,23 +96,20 @@ if($data) {
|
||||
->t('Error getting PHOTO property.'));
|
||||
}
|
||||
$property->setValue(strval($image));
|
||||
$property->parameters = [];
|
||||
/*$property->ENCODING = 'b';
|
||||
$property->TYPE = $type;*/
|
||||
$property->parameters[]
|
||||
= new Sabre\VObject\Parameter('ENCODING', 'b');
|
||||
= new \Sabre\VObject\Parameter('ENCODING', 'b');
|
||||
$property->parameters[]
|
||||
= new Sabre\VObject\Parameter('TYPE', $image->mimeType());
|
||||
= new \Sabre\VObject\Parameter('TYPE', $image->mimeType());
|
||||
$contact->PHOTO = $property;
|
||||
} else {
|
||||
\OCP\Util::writeLog('contacts',
|
||||
'savecrop.php: files: Adding PHOTO property.',
|
||||
\OCP\Util::DEBUG);
|
||||
// For vCard 3.0 the type must be e.g. JPEG or PNG
|
||||
// For version 4.0 the full mimetype should be used.
|
||||
// https://tools.ietf.org/html/rfc2426#section-3.1.4
|
||||
$type = strval($contact->VERSION) === '4.0'
|
||||
? $image->mimeType()
|
||||
: strtoupper(array_pop(explode('/', $image->mimeType())));
|
||||
$contact->add('PHOTO',
|
||||
$image->__toString(), array('ENCODING' => 'b',
|
||||
strval($image), array('ENCODING' => 'b',
|
||||
'TYPE' => $type));
|
||||
}
|
||||
if(!$contact->save()) {
|
||||
@ -111,10 +118,8 @@ if($data) {
|
||||
$thumbnail = $contact->cacheThumbnail($image);
|
||||
\OCP\JSON::success(array(
|
||||
'data' => array(
|
||||
'width' => $image->width(),
|
||||
'height' => $image->height(),
|
||||
'id' => $contactid,
|
||||
'thumbnail' => $thumbnail,
|
||||
'lastmodified' => App::lastModified($contact)->format('U')
|
||||
)
|
||||
));
|
||||
} else {
|
||||
|
@ -29,13 +29,22 @@ OCP\JSON::callCheck();
|
||||
OCP\JSON::setContentTypeHeader('text/plain; charset=utf-8');
|
||||
require_once 'loghandler.php';
|
||||
$l10n = OCA\Contacts\App::$l10n;
|
||||
|
||||
$contactid = isset($_POST['contactid']) ? $_POST['contactid'] : '';
|
||||
$addressbookid = isset($_POST['addressbookid']) ? $_POST['addressbookid'] : '';
|
||||
$backend = isset($_POST['backend']) ? $_POST['backend'] : '';
|
||||
|
||||
if($contactid == '') {
|
||||
bailOut('Missing contact id.');
|
||||
}
|
||||
|
||||
if($addressbookid == '') {
|
||||
bailOut('Missing address book id.');
|
||||
}
|
||||
|
||||
// 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) {
|
||||
if (!isset($_GET['id'])) {
|
||||
bailOut($l10n->t('No contact ID was submitted.'));
|
||||
}
|
||||
$id = $_GET['id'];
|
||||
$tmpkey = 'contact-photo-'.md5($fn);
|
||||
$data = file_get_contents('php://input');
|
||||
$image = new OC_Image();
|
||||
@ -50,10 +59,14 @@ if ($fn) {
|
||||
if(OC_Cache::set($tmpkey, $image->data(), 600)) {
|
||||
OCP\JSON::success(array(
|
||||
'data' => array(
|
||||
'mime'=>$_SERVER['CONTENT_TYPE'],
|
||||
'name'=>$fn,
|
||||
'id'=>$id,
|
||||
'tmp'=>$tmpkey)));
|
||||
'mime'=> $_SERVER['CONTENT_TYPE'],
|
||||
'name'=> $fn,
|
||||
'contactid'=> $id,
|
||||
'addressbookid'=> addressbookid,
|
||||
'backend'=> $backend,
|
||||
'tmp'=>$tmpkey
|
||||
))
|
||||
);
|
||||
exit();
|
||||
} else {
|
||||
bailOut($l10n->t('Couldn\'t save temporary image: ').$tmpkey);
|
||||
@ -63,10 +76,6 @@ if ($fn) {
|
||||
}
|
||||
}
|
||||
|
||||
// Uploads from file dialog are handled here.
|
||||
if (!isset($_POST['id'])) {
|
||||
bailOut($l10n->t('No contact ID was submitted.'));
|
||||
}
|
||||
if (!isset($_FILES['imagefile'])) {
|
||||
bailOut($l10n->t('No file was uploaded. Unknown error'));
|
||||
}
|
||||
@ -101,9 +110,14 @@ if(file_exists($file['tmp_name'])) {
|
||||
'mime'=>$file['type'],
|
||||
'size'=>$file['size'],
|
||||
'name'=>$file['name'],
|
||||
'id'=>$_POST['id'],
|
||||
'tmp'=>$tmpkey,
|
||||
)));
|
||||
),
|
||||
'metadata' => array(
|
||||
'contactid'=> $contactid,
|
||||
'addressbookid'=> $addressbookid,
|
||||
'backend'=> $backend,
|
||||
),
|
||||
));
|
||||
exit();
|
||||
} else {
|
||||
bailOut($l10n->t('Couldn\'t save temporary image: ').$tmpkey);
|
||||
|
28
appinfo/classpath.php
Normal file
28
appinfo/classpath.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
OC::$CLASSPATH['OCA\Contacts\App'] = 'contacts/lib/app.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\AddressBook'] = 'contacts/lib/addressbook.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Contact'] = 'contacts/lib/contact.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\AddressbookLegacy'] = 'contacts/lib/addressbooklegacy.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\IPIMObject'] = 'contacts/lib/ipimobject.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\PIMCollectionAbstract'] = 'contacts/lib/abstractpimcollection.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\PIMObjectAbstract'] = 'contacts/lib/abstractpimobject.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\VCard'] = 'contacts/lib/vcard.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Hooks'] = 'contacts/lib/hooks.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Request'] = 'contacts/lib/request.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Utils\JSONSerializer'] = 'contacts/lib/utils/jsonserializer.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Utils\Properties'] = 'contacts/lib/utils/properties.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Backend\AbstractBackend'] = 'contacts/lib/backend/abstractbackend.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Backend\Database'] = 'contacts/lib/backend/database.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Backend\Shared'] = 'contacts/lib/backend/shared.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Share_Backend_Contact'] = 'contacts/lib/share/contact.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\Share_Backend_Addressbook'] = 'contacts/lib/share/addressbook.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\AddressbookProvider'] = 'contacts/lib/addressbookprovider.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\VObject\VCard'] = 'contacts/lib/vobject/vcard.php';
|
||||
//OC::$CLASSPATH['OCA\Contacts\VObject\StringProperty'] = 'contacts/lib/vobject/stringproperty.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\CardDAV\Backend'] = 'contacts/lib/carddav/backend.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\CardDAV\Plugin'] = 'contacts/lib/carddav/plugin.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\CardDAV\AddressBookRoot'] = 'contacts/lib/carddav/addressbookroot.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\CardDAV\UserAddressBooks'] = 'contacts/lib/carddav/useraddressbooks.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\CardDAV\AddressBook'] = 'contacts/lib/carddav/addressbook.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\CardDAV\Card'] = 'contacts/lib/carddav/card.php';
|
||||
OC::$CLASSPATH['OCA\Contacts\SearchProvider'] = 'contacts/lib/search.php';
|
34
js/app.js
34
js/app.js
@ -485,7 +485,7 @@ OC.Contacts = OC.Contacts || {
|
||||
|
||||
$(document).bind('request.select.contactphoto.fromlocal', function(e, metadata) {
|
||||
console.log('request.select.contactphoto.fromlocal', metadata);
|
||||
$('#contactphoto_fileupload').trigger('click');
|
||||
$('#contactphoto_fileupload').trigger('click', metadata);
|
||||
});
|
||||
|
||||
$(document).bind('request.select.contactphoto.fromcloud', function(e, metadata) {
|
||||
@ -626,6 +626,13 @@ OC.Contacts = OC.Contacts || {
|
||||
$('body').bind('click', bodyListener);
|
||||
}
|
||||
});
|
||||
$('#contactphoto_fileupload').on('click', function(event, metadata) {
|
||||
var form = $('#file_upload_form');
|
||||
form.find('input[name="contactid"]').val(metadata.contactid);
|
||||
form.find('input[name="addressbookid"]').val(metadata.addressbookid);
|
||||
form.find('input[name="backend"]').val(metadata.backend);
|
||||
});
|
||||
|
||||
$('#contactphoto_fileupload').on('change', function() {
|
||||
self.uploadPhoto(this.files);
|
||||
});
|
||||
@ -1588,7 +1595,6 @@ OC.Contacts = OC.Contacts || {
|
||||
var file = filelist[0];
|
||||
var target = $('#file_upload_target');
|
||||
var form = $('#file_upload_form');
|
||||
form.find('input[name="id"]').val(this.currentid);
|
||||
var totalSize=0;
|
||||
if(file.size > $('#max_upload').val()){
|
||||
OC.notify({
|
||||
@ -1602,7 +1608,10 @@ OC.Contacts = OC.Contacts || {
|
||||
var response=jQuery.parseJSON(target.contents().text());
|
||||
if(response != undefined && response.status == 'success') {
|
||||
console.log('response', response);
|
||||
self.editPhoto(self.currentid, response.data.tmp);
|
||||
self.editPhoto(
|
||||
response.metadata,
|
||||
response.data.tmp
|
||||
);
|
||||
//alert('File: ' + file.tmp + ' ' + file.name + ' ' + file.mime);
|
||||
} else {
|
||||
OC.notify({message:response.data.message});
|
||||
@ -1619,7 +1628,7 @@ OC.Contacts = OC.Contacts || {
|
||||
if(jsondata.status == 'success') {
|
||||
//alert(jsondata.data.page);
|
||||
self.editPhoto(metadata, jsondata.data.tmp);
|
||||
$('#edit_photo_dialog_img').html(jsondata.data.page);
|
||||
//$('#edit_photo_dialog_img').html(jsondata.data.page);
|
||||
}
|
||||
else{
|
||||
OC.notify({message: jsondata.data.message});
|
||||
@ -1666,7 +1675,7 @@ OC.Contacts = OC.Contacts || {
|
||||
var $dlg = this.$cropBoxTmpl.octemplate(
|
||||
{
|
||||
backend: metadata.backend,
|
||||
addressbookid: metadata.parent,
|
||||
addressbookid: metadata.addressbookid,
|
||||
contactid: metadata.contactid,
|
||||
tmpkey: tmpkey
|
||||
});
|
||||
@ -1709,25 +1718,26 @@ OC.Contacts = OC.Contacts || {
|
||||
});
|
||||
}).error(function () {
|
||||
OC.notify({message:t('contacts','Error loading profile picture.')});
|
||||
}).attr('src', OC.linkTo('contacts', 'tmpphoto.php')+'?tmpkey='+tmpkey);
|
||||
}).attr('src', OC.linkTo('contacts', 'tmpphoto.php')+'?tmpkey='+tmpkey+'&refresh='+Math.random());
|
||||
},
|
||||
savePhoto:function($dlg) {
|
||||
var form = $dlg.find('#cropform');
|
||||
q = form.serialize();
|
||||
console.log('savePhoto', q);
|
||||
$.post(OC.filePath('contacts', 'ajax', 'savecrop.php'), q, function(response) {
|
||||
var jsondata = $.parseJSON(response);
|
||||
console.log('savePhoto, jsondata', typeof jsondata);
|
||||
if(jsondata && jsondata.status === 'success') {
|
||||
//var jsondata = $.parseJSON(response);
|
||||
console.log('savePhoto, response', typeof response);
|
||||
if(response && response.status === 'success') {
|
||||
// load cropped photo.
|
||||
$(document).trigger('status.contact.photoupdated', {
|
||||
id: jsondata.data.id
|
||||
id: response.data.id,
|
||||
thumbnail: response.data.thumbnail
|
||||
});
|
||||
} else {
|
||||
if(!jsondata) {
|
||||
if(!response) {
|
||||
OC.notify({message:t('contacts', 'Network or server error. Please inform administrator.')});
|
||||
} else {
|
||||
OC.notify({message: jsondata.data.message});
|
||||
OC.notify({message: response.data.message});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user