Merge branch 'dev' of ../p3 into dev

This commit is contained in:
nobody 2020-08-17 23:36:35 -07:00
commit c0f295f5c1
3 changed files with 184 additions and 7 deletions

56
Zotlabs/Lib/Hashpath.php Normal file
View file

@ -0,0 +1,56 @@
<?php
namespace Zotlabs\Lib;
/*
* Zotlabs\Lib\Hashpath
*
* Creates hashed directory structures for fast access and resistance to overloading any single directory with files.
*
* Takes a $url which could be any string
* a $prefix which is where to place the hash directory in the filesystem, default is current directory
* use an empty string for $prefix to place hash directories directly off the root directory
* an optional $depth to indicate the hash level
* $depth = 1, 256 directories, suitable for < 384K records/files
* $depth = 2, 65536 directories, suitable for < 98M records/files
* $depth = 3, 16777216 directories, suitable for < 2.5B records/files
* ...
* The total number of records anticipated divided by the number of hash directories should generally be kept to
* less than 1500 entries for optimum performance though this varies by operating system and filesystem type.
* ext4 uses 32 bit inode numbers (~4B record limit) so use caution or alternative filesystem types with $depth above 3.
* an optional $mkdir (boolean) to recursively create the directory (ignoring errors) before returning
*
* examples: for a $url of 'abcdefg' and prefix of 'path' the following paths are returned for $depth = 1 and $depth = 3
* path/7d/7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a
* path/7d/1a/54/7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a
*
* see also: boot.php:os_mkdir() - here we provide the equivalent of mkdir -p with permissions of 770.
*
*/
class Hashpath {
static function path($url, $prefix = '.', $depth = 1, $mkdir = true) {
$hash = hash('sha256', $url);
$start = 0;
$slice = 2;
if ($depth < 1) {
$depth = 1;
}
$sluglen = $depth * $slice;
do {
$slug = substr($hash,$start,$slice);
$prefix .= '/' . $slug;
$start += $slice;
$sluglen -= $slice;
}
while ($sluglen);
if ($mkdir) {
os_mkdir($prefix, STORAGE_DEFAULT_PERMISSIONS, true);
}
return $prefix . '/' . $hash;
}
}

View file

@ -1,21 +1,18 @@
<?php
namespace Zotlabs\Lib;
use Zotlabs\Lib\Hashpath;
use Zotlabs\Daemon\Run;
class Img_cache {
static $cache_life = 18600 * 7;
static function get_filename($url, $prefix = EMPTY_STR, $sluglen = 2) {
$hash = hash('sha256', $url);
$slug = substr($hash,0,2);
$path = $prefix . '/' . $slug;
os_mkdir($path, STORAGE_DEFAULT_PERMISSIONS, true);
return $path . '/' . $hash;
static function get_filename($url, $prefix = '.') {
return Hashpath::hash($url,$prefix);
}
static function check($url, $prefix = EMPTY_STR) {
static function check($url, $prefix = '.') {
if (strpos($url,z_root()) !== false) {
return false;

View file

@ -311,6 +311,130 @@ function import_xchan_photo($photo, $xchan, $thing = false, $force = false) {
return([$photo, $thumb, $micro, $type, $failed, $modified]);
}
function import_remote_xchan_photo($photo, $xchan) {
// logger('Updating channel photo from ' . $photo . ' for ' . $xchan, LOGGER_DEBUG);
$failed = true;
$hash = hash('sha256', $photo);
$slug = substr($hash,0,2);
$slug2 = substr($hash,2,2);
$path = 'cache/xphoto/' . $slug . '/' . $slug2;
$outfile = $path . '/' . $hash;
os_mkdir($path, STORAGE_DEFAULT_PERMISSIONS, true);
$modified = ((file_exists($outfile)) ? @filemtime($outfile) : 0);
if (strpos($photo,z_root() === 0)) {
return false;
}
if ($modified) {
$h = [ 'headers' => [ 'If-Modified-Since: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', $modified) . ' GMT' ] ];
$recurse = 0;
$result = z_fetch_url($photo, true, $recurse, $h);
}
else {
$result = z_fetch_url($photo, true);
}
if ($result['success']) {
$type = guess_image_type($photo, $result['header']);
if ($type) {
$failed = false;
}
}
elseif ($result['return_code'] == 304) {
$photo = z_root() . '/photo/' . $hash . '-4';
$thumb = z_root() . '/photo/' . $hash . '-5';
$micro = z_root() . '/photo/' . $hash . '-6';
$failed = false;
}
if (! $failed && $result['return_code'] != 304) {
$img = photo_factory($img_str, $type);
if ($img->is_valid()) {
$width = $img->getWidth();
$height = $img->getHeight();
if ($width && $height) {
if (($width / $height) > 1.2) {
// crop out the sides
$margin = $width - $height;
$img->cropImage(300, ($margin / 2), 0, $height, $height);
}
elseif (($height / $width) > 1.2) {
// crop out the bottom
$margin = $height - $width;
$img->cropImage(300, 0, 0, $width, $width);
}
else {
$img->scaleImageSquare(300);
}
}
else {
$failed = true;
}
$p = [
'xchan' => $xchan,
'resource_id' => $hash,
'filename' => basename($photo),
'album' => $album,
'photo_usage' => $flags,
'imgscale' => 4,
'edited' => $modified,
];
$r = $img->save($p);
if ($r === false) {
$failed = true;
}
$img->scaleImage(80);
$p['imgscale'] = 5;
$r = $img->save($p);
if ($r === false) {
$failed = true;
}
$img->scaleImage(48);
$p['imgscale'] = 6;
$r = $img->save($p);
if ($r === false) {
$failed = true;
}
$photo = z_root() . '/photo/' . $hash . '-4';
$thumb = z_root() . '/photo/' . $hash . '-5';
$micro = z_root() . '/photo/' . $hash . '-6';
}
else {
logger('Invalid image from ' . $photo);
$failed = true;
}
}
if ($failed) {
$default = get_default_profile_photo();
$photo = z_root() . '/' . $default;
$thumb = z_root() . '/' . get_default_profile_photo(80);
$micro = z_root() . '/' . get_default_profile_photo(48);
$type = 'image/png';
$modified = gmdate('Y-m-d H:i:s', filemtime($default));
}
logger('HTTP code: ' . $result['return_code'] . '; modified: ' . $modified
. '; failure: ' . ($failed ? 'yes' : 'no') . '; URL: ' . $photo, LOGGER_DEBUG);
return([$photo, $thumb, $micro, $type, $failed, $modified]);
}
/**
* @brief Import channel photo from a URL.
*