📄 galleryresources.class.php
字号:
function addResourceToDatabase( $ownerId, $albumId, $description, $flags, $resourceType, $filePath, $fileName, $metadata ) { // prepare the metadata to be stored in the db $fileSize = $metadata["filesize"]; $serMetadata = Db::qstr( serialize($metadata)); // get the correct thumbnail format lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); $config =& Config::getConfig(); $thumbnailFormat = $config->getValue( "thumbnail_format" ); // prepare some other stuff lt_include( PLOG_CLASS_PATH."class/data/textfilter.class.php" ); $tf = new Textfilter(); $normalizedDescription = $tf->normalizeText( $description ); $properties = serialize( array() ); // check if there already is a file with the same name stored $duplicated = $this->isDuplicatedFileName( $fileName ); // finally put the query together and execute it $query = "INSERT INTO ".$this->getPrefix()."gallery_resources( owner_id, album_id, description, flags, resource_type, file_path, file_name, file_size, metadata, thumbnail_format, normalized_description, properties) VALUES ( $ownerId, $albumId, '".Db::qstr($description)."', $flags, $resourceType, '$filePath', '$fileName', '$fileSize', '$serMetadata', '$thumbnailFormat', '".Db::qstr($normalizedDescription)."', '$properties');"; $result = $this->Execute( $query ); // check the return result if( !$result ) return GALLERY_ERROR_ADDING_RESOURCE; // get the id that was given to the record $resourceId = $this->_db->Insert_ID(); // check if we have two resources with the same filename now // check if there already exists a file with the same name // // if that's the case, then we should rename the one we just // added with some random prefix, to make it different from the // other one... if( $duplicated ) { $query = "UPDATE ".$this->getPrefix()."gallery_resources SET file_name = '$resourceId-$fileName' WHERE id = $resourceId"; $this->Execute( $query ); } // clear our own caches $this->_cache->removeData( $resourceId, CACHE_RESOURCES ); $this->_cache->removeData( $ownerId, CACHE_RESOURCES_USER ); $this->_cache->removeData( $fileName, CACHE_RESOURCES_BY_NAME ); return $resourceId; } /** * @private * @param fileName * @param metadata */ function _getResourceType( $fileName, &$metadata ) { // find out the right resource type based on the extension // get the resource type $fileParts = explode( ".", $fileName ); $fileExt = strtolower($fileParts[count($fileParts)-1]); //asf need special working if ("asf" == $fileExt ){ if (!($metadata["audio"]["codec"])) $resourceType = GALLERY_RESOURCE_SOUND; else $resourceType = GALLERY_RESOURCE_VIDEO; } else { if( array_key_exists( $fileExt, $this->_extensionToType )) $resourceType = $this->_extensionToType[ $fileExt ]; else $resourceType = GALLERY_RESOURCE_UNKNOWN; } return( $resourceType ); } /** * adds a resource to the database. This method requires a FileUpload parameter and it * will take care of processing the upload file and so on. If the file is already in disk and we'd * like to add it, please check GalleryResources::addResourceFromDisk() * This method will also take care of extracting the metadata from the file and generating the * thumbnail in the required format, according to our configuration. * * @param ownerId * @param albumId * @param description * @param upload A FileUpload object * @see FileUpload * @see GalleryResources::addResourceFromDisk() * @return It will return one of the following constants: * - GALLERY_ERROR_RESOURCE_TOO_BIG * - GALLERY_ERROR_RESOURCE_FORBIDDEN_EXTENSION * - GALLERY_ERROR_QUOTA_EXCEEDED * - GALLERY_ERROR_ADDING_RESOURCE * - GALLERY_ERROR_UPLOADS_NOT_ENABLED * or the identifier of the resource that was just added if the operation succeeded. */ function addResource( $ownerId, $albumId, $description, $upload ) { // check if quotas are enabled, and if this file would make us go // over the quota lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryresourcequotas.class.php" ); if( GalleryResourceQuotas::isBlogOverResourceQuota( $ownerId, $upload->getSize())) { return GALLERY_ERROR_QUOTA_EXCEEDED; } // first of all, validate the file using the // upload validator class. It can return // UPLOAD_VALIDATOR_ERROR_UPLOAD_TOO_BIG (-1) // or // UPLOAD_VALIDATOR_ERROR_FORBIDDEN_EXTENSION (-2) // in case the file is not valid. lt_include( PLOG_CLASS_PATH."class/data/validator/uploadvalidator.class.php" ); $uploadValidator = new UploadValidator(); $error = $uploadValidator->validate( $upload ); if( $error < 0 ) return $error; // get the metadata lt_include( PLOG_CLASS_PATH."class/gallery/getid3/getid3.php" ); $getId3 = new GetID3(); $metadata = $getId3->analyze( $upload->getTmpName()); // nifty helper method from the getid3 package getid3_lib::CopyTagsToComments($metadata); $resourceType = $this->_getResourceType( $upload->getFileName(), $metadata ); // set the flags $flags = 0; if( $resourceType == GALLERY_RESOURCE_IMAGE ) $flags = $flags|GALLERY_RESOURCE_PREVIEW_AVAILABLE; $info = $this->_filterMetadata( $metadata, $resourceType ); // add the record to the database $fileName = $upload->getFileName(); $duplicated = $this->isDuplicatedFilename( $fileName ); $filePath = ""; $resourceId = $this->addResourceToDatabase( $ownerId, $albumId, $description, $flags, $resourceType, $filePath, $fileName, $info ); if( !$resourceId ) return false; if( $duplicated ) { $upload->setFileName( $resourceId."-".$upload->getFileName()); } // and finally move the file to the right place in disk // move the file to disk lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryresourcestorage.class.php" ); $storage = new GalleryResourceStorage(); $resFile = $storage->storeUpload( $resourceId, $ownerId, $upload ); // if the file cannot be read, we will also remove the record from the // database so that we don't screw up $fileReadable = File::isReadable( $resFile ); if( !$resFile || $resFile < 0 || !$fileReadable ) { // if something went wrong, we should not keep the record in the db $query = "DELETE FROM ".$this->getPrefix()."gallery_resources WHERE id = $resourceId"; $this->Execute( $query ); return $resFile; } lt_include( PLOG_CLASS_PATH."class/gallery/dao/galleryalbums.class.php" ); $albums = new GalleryAlbums(); $album = $albums->getAlbum( $albumId ); $album->setNumResources( $album->getNumResources() + 1 ); $albums->updateAlbum( $album ); // and finally, we can generate the thumbnail only if the file is an image, of course :) if( $resourceType == GALLERY_RESOURCE_IMAGE ) { lt_include( PLOG_CLASS_PATH."class/gallery/resizers/gallerythumbnailgenerator.class.php" ); lt_include( PLOG_CLASS_PATH."class/config/config.class.php" ); $config =& Config::getConfig(); $imgWidth = $info["video"]["resolution_x"]; $imgHeight = $info["video"]["resolution_y"]; $previewHeight = $config->getValue( "thumbnail_height", GALLERY_DEFAULT_THUMBNAIL_HEIGHT ); $previewWidth = $config->getValue( "thumbnail_width", GALLERY_DEFAULT_THUMBNAIL_WIDTH ); $thumbHeight = ( $imgHeight > $previewHeight ? $previewHeight : $imgHeight ); $thumbWidth = ( $imgWidth > $previewWidth ? $previewWidth : $imgWidth ); GalleryThumbnailGenerator::generateResourceThumbnail( $resFile, $resourceId, $ownerId, $thumbHeight, $thumbWidth ); $medPreviewHeight = $config->getValue( "medium_size_thumbnail_height", GALLERY_DEFAULT_MEDIUM_SIZE_THUMBNAIL_HEIGHT ); $medPreviewWidth = $config->getValue( "medium_size_thumbnail_width", GALLERY_DEFAULT_MEDIUM_SIZE_THUMBNAIL_WIDTH ); $thumbHeight = ( $imgHeight > $medPreviewHeight ? $medPreviewHeight : $imgHeight ); $thumbWidth = ( $imgWidth > $medPreviewWidth ? $medPreviewWidth : $imgWidth ); GalleryThumbnailGenerator::generateResourceMediumSizeThumbnail( $resFile, $resourceId, $ownerId, $thumbHeight, $thumbWidth ); // call this method only if the settings are right and the image is bigger than the final size(s) $finalPreviewHeight = $config->getValue( "final_size_thumbnail_height", 0 ); $finalPreviewWidth = $config->getValue( "final_size_thumbnail_width", 0 ); if( $finalPreviewHeight > 0 ) if( $imgHeight < $finalPreviewHeight ) $finalPreviewHeight = $imgHeight; if( $finalPreviewWidth > 0 ) if( $imgWidth < $finalPreviewWidth ) $finalPreviewWidth = $imgWidth; if( $finalPreviewHeight != 0 && $finalPreviewWidth != 0 ) { GalleryThumbnailGenerator::generateResourceFinalSizeThumbnail( $resFile, $resourceId, $ownerId, $finalPreviewHeight, $finalPreviewWidth ); // we have to recalculate the metadata because the image could be different... This is a bit cumbersome // and repeats code. We know, thanks. $getId3 = new GetID3(); $metadata = $getId3->analyze( $resFile ); getid3_lib::CopyTagsToComments($metadata); $info = $this->_filterMetadata( $metadata, $resourceType ); // and finally update the resource again $resource = $this->getResource( $resourceId ); $resource->setMetadata( $info ); $this->updateResource( $resource ); } } // return the id of the resource we just added return $resourceId; } /** * @private * Returns an array with only those bits of metadata as generate by getid3 that * we'd like to keep, instead of one huge array * * @param metadata * @param resourceType */ function _filterMetadata( &$metadata, $resourceType ) { $info = Array(); if( isset( $metadata["md5_file"] )) $info["md5_file"] = $metadata["md5_file"]; else $info["md5_file"] = ""; if( isset( $metadata["md5_data"] )) $info["md5_data"] = $metadata["md5_data"]; else $info["md5_data"] = ""; if( isset( $metadata["filesize"] )) $info["filesize"]= $metadata["filesize"]; else $info["filesize"] = 0; if( isset( $metadata["fileformat"] )) $info["fileformat"] = $metadata["fileformat"]; else $metadata["fileformat"] = ""; if( isset( $metadata["comments"] )) $info["comments"] = $metadata["comments"]; else $info["comments"] = 0; if($resourceType == GALLERY_RESOURCE_IMAGE){ if( isset( $metadata["video"] )) $info["video"] = $metadata["video"]; if( isset( $metadata["jpg"] )) { $info["jpg"]["exif"]["FILE"] = $metadata["jpg"]["exif"]["FILE"]; $info["jpg"]["exif"]["COMPUTED"] = $metadata["jpg"]["exif"]["COMPUTED"]; if(isset( $metadata["jpg"]["exif"]["IFD0"] )) $info["jpg"]["exif"]["IFD0"] = $metadata["jpg"]["exif"]["IFD0"]; $metadata["jpg"]["exif"]["EXIF"]["MakerNote"] = ""; $info["jpg"]["exif"]["EXIF"] = $metadata["jpg"]["exif"]["EXIF"]; } } else if($resourceType == GALLERY_RESOURCE_SOUND){ $info["audio"] = $metadata["audio"]; $info["playtime_string"] = $metadata["playtime_string"]; $info["playtime_seconds"] = $metadata["playtime_seconds"]; } else if($resourceType == GALLERY_RESOURCE_VIDEO){ $info["video"] = $metadata["video"]; $info["audio"] = $metadata["audio"]; $info["playtime_seconds"] = $metadata["playtime_seconds"]; $info["playtime_string"] = $metadata["playtime_string"]; } else if( $resourceType == GALLERY_RESOURCE_ZIP ) { $info["zip"]["compressed_size"] = $metadata["zip"]["compressed_size"]; $info["zip"]["uncompressed_size"] = $metadata["zip"]["uncompressed_size"]; $info["zip"]["entries_count"] = $metadata["zip"]["entries_count"]; $info["zip"]["compression_method"] = $metadata["zip"]["compression_method"]; $info["zip"]["compression_speed"] = $metadata["zip"]["compression_speed"]; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -