From: <ale...@us...> - 2008-05-29 14:39:23
|
Revision: 1374 http://gallery-contrib.svn.sourceforge.net/gallery-contrib/?rev=1374&view=rev Author: alecmyers Date: 2008-05-29 07:39:25 -0700 (Thu, 29 May 2008) Log Message: ----------- First Upload Added Paths: ----------- trunk/gallery2/modules/downloadlink/ trunk/gallery2/modules/downloadlink/OfferDownload.inc trunk/gallery2/modules/downloadlink/module.inc Added: trunk/gallery2/modules/downloadlink/OfferDownload.inc =================================================================== --- trunk/gallery2/modules/downloadlink/OfferDownload.inc (rev 0) +++ trunk/gallery2/modules/downloadlink/OfferDownload.inc 2008-05-29 14:39:25 UTC (rev 1374) @@ -0,0 +1,95 @@ +<?php +/* + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2007 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ + +GalleryCoreApi::requireOnce('modules/core/DownloadItem.inc'); + +/** + * Provides downloading of binary items as files + * @package DownloadLink + * @author Bharat Mediratta <bh...@me...> + * @author Alec Myers <al...@al...> + */ +class OfferDownloadView extends DownloadItemView { + + function _sendFile($data) { + global $gallery; + $platform =& $gallery->getPlatform(); + $session =& $gallery->getSession(); + $phpVm = $gallery->getPhpVm(); + + if (function_exists('apache_setenv')) { + @apache_setenv('no-gzip', '1'); + } + + /* Make sure we have permission */ + $permission = 'downloadlink.original'; + switch ($data['derivativeType']) { + case DERIVATIVE_TYPE_IMAGE_THUMBNAIL: + $permission = 'downloadlink.thumbnail'; + break; + + case DERIVATIVE_TYPE_IMAGE_RESIZE: + $permission = 'downloadlink.resizes'; + break; + + /* DERIVATIVE_TYPE_IMAGE_PREFERRED uses core.viewSource */ + } + + $ret = GalleryCoreApi::assertHasItemPermission($data['parentId'], $permission); + if ($ret) { + return $ret; + } + + $requestMethod = strtolower(GalleryUtilities::getServerVar('REQUEST_METHOD')); + $phpVm->header('Content-Type: application/octet-stream'); + $phpVm->header('Content-Disposition: attachment; filename="' . $data['pseudoFileName'] . '"'); + $stats = $platform->stat($data['derivativePath']); + $phpVm->header('Last-Modified: ' . GalleryUtilities::getHttpDate($stats[9])); + $phpVm->header('Expires: ' . GalleryUtilities::getHttpDate(time() + 31536000)); + + /* If the request method is HEAD, don't send back the body */ + if ($requestMethod == 'head') { + $phpVm->header('Content-length: 0'); + } else { + if ($stats[7] > 0) { + $phpVm->header('Content-length: ' . $stats[7]); + } + /* + * Don't use readfile() because it buffers the entire file in memory. Profiling shows + * that this approach is as efficient as fpassthru() but we get to call + * guaranteeTimeLimit which prevents it from failing on very large files + */ + if ($fd = $platform->fopen($data['derivativePath'], 'rb')) { + while (true) { + $bits = $platform->fread($fd, 65535); + if (strlen($bits) == 0) { + break; + } + print $bits; + $gallery->guaranteeTimeLimit(30); + } + $platform->fclose($fd); + } + } + + return null; + } +} +?> Property changes on: trunk/gallery2/modules/downloadlink/OfferDownload.inc ___________________________________________________________________ Name: svn:keywords + Author Id Revision Name: svn:eol-style + native Added: trunk/gallery2/modules/downloadlink/module.inc =================================================================== --- trunk/gallery2/modules/downloadlink/module.inc (rev 0) +++ trunk/gallery2/modules/downloadlink/module.inc 2008-05-29 14:39:25 UTC (rev 1374) @@ -0,0 +1,144 @@ +<?php +/* + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2007 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ + + /** + * Download module + * + * Provides downloading of binary items as files + * @package Download + * @author Alec Myers <al...@al...> + */ + + class DownloadLinkModule extends GalleryModule { + + function DownloadLinkModule() { + global $gallery; + + $this->setId('downloadlink'); + $this->setName($gallery->i18n('Download Link')); + $this->setDescription($gallery->i18n("Provides a link to download a picture")); + $this->setVersion('0.1.0'); + $this->setGroup('export', $gallery->i18n('Export')); + $this->setCallbacks('getItemLinks'); + $this->setRequiredCoreApi(array(7, 10)); + $this->setRequiredModuleApi(array(3, 2)); + } + + /** + * @see GalleryModule::getItemLinks() + */ + + function upgrade($currentVersion) { + global $gallery; + + + if (!isset($currentVersion)) { + /* Initial install.... */ + list ($ret, $coreParams) = GalleryCoreApi::fetchAllPluginParameters('module', 'core'); + if ($ret) { + return $ret; + } + /* register permissions */ + $permissions[] = array('original', $gallery->i18n('[downloadlink] Show Download Original Link'), 0, array()); + $permissions[] = array('resizes', $gallery->i18n('[downloadlink] Show Download Resize(s) Links'), 0, array()); + $permissions[] = array('thumbnail', $gallery->i18n('[downloadlink] Show Download Thumbnail Link'), 0, array()); + foreach ($permissions as $p) { + $ret = GalleryCoreApi::registerPermission($this->getId(),'downloadlink.'.$p[0], $p[1], $p[2], $p[3]); + if ($ret) { + return $ret; + } + } + /* Give everybody download permissions by default */ + $ret = GalleryCoreApi::addGroupPermission($coreParams['id.rootAlbum'], $coreParams['id.everybodyGroup'],'downloadlink.original', true); + if ($ret) { + return $ret; + } + $ret = GalleryCoreApi::addGroupPermission($coreParams['id.rootAlbum'], $coreParams['id.everybodyGroup'],'downloadlink.resizes', true); + if ($ret) { + return $ret; + } + } + + return null; + } + + + function getItemLinks($items, $wantsDetailedLinks, $permissions) { + $links = array(); + foreach ($items as $item) { + if (!GalleryUtilities::isA($item, 'GalleryPhotoItem')) { + continue; + } + $masterId = $item->getId(); + + $potentialImages = array(); + + /* try thumbnail */ + $ret = GalleryCoreApi::assertHasItemPermission($masterId,'downloadlink.thumbnail'); + if (!$ret) { + list($ret,$thumbs) = GalleryCoreApi::fetchThumbnailsByItemIds( array($masterId) ); + if ($ret) { + return array ($ret,null); + } + $potentialImages[] = $thumbs[$masterId]; + } + /* resizes */ + $ret = GalleryCoreApi::assertHasItemPermission($masterId,'downloadlink.resizes'); + if (!$ret) { + list ($ret, $resizes) = GalleryCoreApi::fetchResizesByItemIds(array($masterId)); + if ($ret) { + return array($ret,null); + } + if (!empty ($resizes[$masterId])) { + foreach ($resizes[$masterId] as $resize) { + $potentialImages[] = $resize; + } + } + } + + /* how about the original? */ + $ret = GalleryCoreApi::assertHasItemPermission($masterId,'downloadlink.original'); + if (!$ret) { + /* fetch preferred */ + list ($ret, $preferreds) = GalleryCoreApi::fetchPreferredsByItemIds( array ($masterId) ); + if ($ret) { + return array($ret,null); + } + if (isset($preferreds[$masterId])) { + $potentialImages[] = $preferreds[$masterId]; + } else { + $potentialImages[] = $item; + } + } + + $itemTypeNames = $item->itemTypeName(); + foreach ($potentialImages as $potentialImage) { + $links[$masterId][] = + array('text' => $this->translate(array('text' => 'Download %s (%s x %s)','arg1' => $itemTypeNames[0], + 'arg2' => $potentialImage->getWidth(),'arg3' => $potentialImage->getHeight())), + 'params' => array('view' => 'downloadlink.OfferDownload', + 'itemId' => $potentialImage->getId())); + } + } + + return array(null, $links); + } +} +?> \ No newline at end of file Property changes on: trunk/gallery2/modules/downloadlink/module.inc ___________________________________________________________________ Name: svn:keywords + Author Id Revision Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |