Skip to content

Commit

Permalink
Merge pull request #371 from owncloud/previews-by-file-id
Browse files Browse the repository at this point in the history
Use the object ID to generate the preview
  • Loading branch information
nickvergessen committed Aug 29, 2015
2 parents 091a6e1 + 7f892b9 commit f79d18a
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 80 deletions.
1 change: 1 addition & 0 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ public function __construct (array $urlParams = array()) {
$server->getDateTimeFormatter(),
$server->getPreviewManager(),
$server->getURLGenerator(),
$server->getMimeTypeDetector(),
new View(''),
$c->query('CurrentUID')
);
Expand Down
86 changes: 60 additions & 26 deletions controller/activities.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\Files;
use OCP\Files\IMimeTypeDetector;
use OCP\IDateTimeFormatter;
use OCP\IPreview;
use OCP\IRequest;
Expand Down Expand Up @@ -64,6 +65,9 @@ class Activities extends Controller {
/** @var IURLGenerator */
protected $urlGenerator;

/** @var mimeTypeDetector */
protected $mimeTypeDetector;

/** @var View */
protected $view;

Expand All @@ -82,6 +86,7 @@ class Activities extends Controller {
* @param IDateTimeFormatter $dateTimeFormatter
* @param IPreview $preview
* @param IURLGenerator $urlGenerator
* @param IMimeTypeDetector $mimeTypeDetector
* @param View $view
* @param string $user
*/
Expand All @@ -94,6 +99,7 @@ public function __construct($appName,
IDateTimeFormatter $dateTimeFormatter,
IPreview $preview,
IURLGenerator $urlGenerator,
IMimeTypeDetector $mimeTypeDetector,
View $view,
$user) {
parent::__construct($appName, $request);
Expand All @@ -104,6 +110,7 @@ public function __construct($appName,
$this->dateTimeFormatter = $dateTimeFormatter;
$this->preview = $preview;
$this->urlGenerator = $urlGenerator;
$this->mimeTypeDetector = $mimeTypeDetector;
$this->view = $view;
$this->user = $user;
}
Expand Down Expand Up @@ -152,22 +159,22 @@ public function fetch($page, $filter = 'all', $objecttype = '', $objectid = 0) {
}

$activity['previews'] = [];
if (!empty($activity['files'])) {
foreach ($activity['files'] as $file) {
if ($file === '') {
if ($activity['object_type'] === 'files' && !empty($activity['files'])) {
foreach ($activity['files'] as $objectId => $objectName) {
if (((int) $objectId) === 0 || $objectName === '') {
// No file, no preview
continue;
}

$activity['previews'][] = $this->getPreview($activity, $file);
$activity['previews'][] = $this->getPreview($activity['affecteduser'], (int) $objectId, $objectName);

if (sizeof($activity['previews']) >= self::MAX_NUM_THUMBNAILS) {
// Don't want to clutter the page, so we stop after a few thumbnails
break;
}
}
} else if ($activity['file'] !== '') {
$activity['previews'][] = $this->getPreview($activity, $activity['file']);
} else if ($activity['object_type'] === 'files' && $activity['object_id']) {
$activity['previews'][] = $this->getPreview($activity['affecteduser'], (int) $activity['object_id'], $activity['file']);
}

$preparedActivities[] = $activity;
Expand All @@ -177,49 +184,76 @@ public function fetch($page, $filter = 'all', $objecttype = '', $objectid = 0) {
}

/**
* @param array $activity
* @param string $file
* @param string $owner
* @param int $fileId
* @param string $filePath
* @return array
*/
protected function getPreview(array $activity, $file) {
$this->view->chroot('/' . $activity['affecteduser'] . '/files');
$exist = $this->view->file_exists($file);
$is_dir = $this->view->is_dir($file);
protected function getPreview($owner, $fileId, $filePath) {
$this->view->chroot('/' . $owner . '/files');
$path = $this->view->getPath($fileId);

if ($path === null || $path === '' || !$this->view->file_exists($path)) {
return $this->getPreviewFromPath($filePath);
}

$is_dir = $this->view->is_dir($path);

$preview = [
'link' => $this->getPreviewLink($file, $is_dir),
'link' => $this->getPreviewLink($path, $is_dir),
'source' => '',
'isMimeTypeIcon' => true,
];

// show a preview image if the file still exists
if ($is_dir) {
$mimeTypeIcon = Template::mimetype_icon('dir');
if (substr($mimeTypeIcon, -4) === '.png') {
$mimeTypeIcon = substr($mimeTypeIcon, 0, -4) . '.svg';
}
$preview['source'] = $mimeTypeIcon;
$preview['source'] = $this->getPreviewPathFromMimeType('dir');
} else {
$mimeType = Files::getMimeType($file);
if (!$is_dir && $mimeType && $this->preview->isMimeSupported($mimeType) && $exist) {
$fileInfo = $this->view->getFileInfo($path);
if ($this->preview->isAvailable($fileInfo)) {
$preview['isMimeTypeIcon'] = false;
$preview['source'] = $this->urlGenerator->linkToRoute('core_ajax_preview', [
'file' => $file,
'file' => $path,
'c' => $this->view->getETag($path),
'x' => 150,
'y' => 150,
]);
} else {
$mimeTypeIcon = Template::mimetype_icon($mimeType);
if (substr($mimeTypeIcon, -4) === '.png') {
$mimeTypeIcon = substr($mimeTypeIcon, 0, -4) . '.svg';
}
$preview['source'] = $mimeTypeIcon;
$preview['source'] = $this->getPreviewPathFromMimeType($fileInfo->getMimetype());
}
}

return $preview;
}

/**
* @param string $filePath
* @return array
*/
protected function getPreviewFromPath($filePath) {
$mimeType = $this->mimeTypeDetector->detectPath($filePath);
$preview = [
'link' => $this->getPreviewLink($filePath, false),
'source' => $this->getPreviewPathFromMimeType($mimeType),
'isMimeTypeIcon' => true,
];

return $preview;
}

/**
* @param string $mimeType
* @return string
*/
protected function getPreviewPathFromMimeType($mimeType) {
$mimeTypeIcon = $this->mimeTypeDetector->mimeTypeIcon($mimeType);
if (substr($mimeTypeIcon, -4) === '.png') {
$mimeTypeIcon = substr($mimeTypeIcon, 0, -4) . '.svg';
}

return $mimeTypeIcon;
}

/**
* @param string $path
* @param bool $isDir
Expand Down
13 changes: 5 additions & 8 deletions lib/grouphelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,17 @@ public function addActivity($activity) {
$this->openGroup['subjectparams_array'][$parameter] = array($this->openGroup['subjectparams_array'][$parameter]);
}
if (!isset($this->openGroup['activity_ids'])) {
$this->openGroup['activity_ids'] = array((int) $this->openGroup['activity_id']);
$this->openGroup['files'] = array((string) $this->openGroup['file']);
$this->openGroup['object_ids'] = array((int) $this->openGroup['object_id']);
$this->openGroup['activity_ids'] = [(int) $this->openGroup['activity_id']];
$this->openGroup['files'] = [
(int) $this->openGroup['object_id'] => (string) $this->openGroup['file']
];
}

$this->openGroup['subjectparams_array'][$parameter][] = $activity['subjectparams_array'][$parameter];
$this->openGroup['subjectparams_array'][$parameter] = array_unique($this->openGroup['subjectparams_array'][$parameter]);
$this->openGroup['activity_ids'][] = (int) $activity['activity_id'];

$objectId = (int) $activity['object_id'];
if (!in_array($objectId, $this->openGroup['object_ids'])) {
$this->openGroup['files'][] = (string) $activity['file'];
$this->openGroup['object_ids'][] = $objectId;
}
$this->openGroup['files'][(int) $activity['object_id']] = (string) $activity['file'];
}
} else {
$this->closeOpenGroup();
Expand Down
Loading

0 comments on commit f79d18a

Please sign in to comment.