兰空图床Lsky Pro增加AWS S3/Minio支持

兰空图床是一款简单而强大的图床程序,但是目前并不支持minio/s3存储策略,作者计划在2.0版本开发此功能。

然而我不想等了,决定略写几行代码,以支持自建minio后端。有需要的看官可以参考。

大约如下几步:

一、撰写AWS S3存储driver,放到extend/strategy/driver/ 目录下:

<?php

namespace strategy\driver;

use strategy\Driver;

if (!function_exists('exif_imagetype')) {
    function exif_imagetype($filename)
    {
        if ((list($width, $height, $type, $attr) = getimagesize($filename)) !== false) {
            return $type;
        }
        return false;
    }
}

/**
 * Aws储存驱动
 *
 * Class Aws
 * @package strategy\driver
 */
class Aws implements Driver
{
    /**
     * 当前储存策略参数
     *
     * @var array
     */
    protected $options = [];

    /**
     * 错误信息
     *
     * @var null
     */
    protected $error = null;

    /**
     * Aws实例
     *
     * @var null
     */
    protected $s3 = null;

    /**
     * Aws constructor.
     *
     * @param array $options
     */
    public function __construct($options = [])
    {
        $this->options = $options;
        try {
            $this->s3 = new \Aws\S3\S3Client([
                'version' => 'latest',
                'region'  => empty($this->options['s3_region']) ? 'us-east-1' : $this->options['s3_region'],
                'endpoint' => $this->options['s3_endpoint'],
                'use_path_style_endpoint' => true,
                'credentials' => [
                    'key'    => $this->options['s3_key'],
                    'secret' => $this->options['s3_secret'],
                ],
            ]);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
        }
    }

    /**
     * 创建文件
     *
     * @param $pathname
     * @param $file
     *
     * @return bool
     */
    public function create($pathname, $file)
    {
        try {
            $params = array(
                'Bucket' => $this->options['s3_bucket'],
                'Key' => $pathname,
                'Body' => fopen($file, 'rb')
            );
            if ($image_type = exif_imagetype($file)) {
                $params['ContentType'] = image_type_to_mime_type($image_type);
            }

            $this->s3->putObject($params);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    /**
     * 删除文件
     *
     * @param $pathname
     *
     * @return bool
     */
    public function delete($pathname)
    {
        try {
            $this->s3->deleteObject([
                'Bucket' => $this->options['s3_bucket'],
                'Key' => $pathname,
            ]);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    /**
     * 删除多个文件
     *
     * @param array $list
     * @return bool|mixed
     */
    public function deletes(array $list)
    {
        try {
            $objects = [];
            foreach ($list as $value) {
                $objects[] = ['Key' => $value ];
            }
            $this->s3->deleteObjects([
                'Bucket' => $this->options['s3_bucket'],
                'Objects' => $objects,
            ]);
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            return false;
        }

        return true;
    }

    public function getError()
    {
        return 'Aws:' . $this->error;
    }
}

二、增加aws-sdk-php依赖

composer require aws/aws-sdk-php -n

三、增加存储策略配置,在config/strategy.php 增加一项

'aws'=>['name'=>'AWS S3','class'=>\strategy\driver\Aws::class],

四、执行SQL增加配置参数

INSERT INTO `lsky_config` VALUES (0,'aws','text','text','s3_endpoint','Endpoint',NULL,'',''),(0,'aws','text','text','s3_key','Key',NULL,'',''),(0,'aws','text','text','s3_secret','Secret',NULL,'',''),(0,'aws','text','text','s3_bucket','Bucket','储存桶名称','',''),(0,'aws','text','text','aws_cdn_domain','域名',NULL,'','');

五,在后台设置存储策略,完成

 

附:构建docker参考脚本 docker.zip

微信小程序Canvas不支持 isPointInPath的解决方案

        近期在做一个贴纸功能,里面需要判断点击坐标是否处于贴纸范围,该贴纸经过了旋转和缩放。html5的canvas支持isPointInPath方法快速判断点击是否属于某多边形区域,但是微信小程序并不支持.

        研究了一番,使用了ray-crossing算法的判断方法如下(在边缘线的也算进去了):

function isPointInPolygon (point, target) {
    const targetSize = target.length;
    let nCross = 0;
    for (let i = 0; i < targetSize; i += 1) {
      const p1 = target[i];
      const p2 = target[(i + 1) % targetSize];

      if (point.y < Math.min(p1.y, p2.y)) continue;
      if (point.y > Math.max(p1.y, p2.y)) continue;

      if (p1.y == p2.y) {
        const minX = Math.min(p1.x, p2.x);
        const maxX = Math.max(p1.x, p2.x);
        if (point.y == p1.y && (point.x >= minX && point.x <= maxX)) {
          return true;
        }
      } else {
        const x = ((point.y - p1.y) * (p2.x - p1.x)) / (p2.y - p1.y) + p1.x;
        if (x == point.x) {
          return true;
        } else if (x > point.x) {
          nCross++;
        }
      }
    }

    return nCross % 2 == 1;
  }

参考文章:https://blog.csdn.net/yanjunmu/article/details/46723407

 

 

CentOS 6/7 x86_64更换4.9版本内核,支持Google BBR拥塞控制算法

该脚本内核由本人编译,脚本特点:
1、无需手动修改grub启动项;
2、已经默认设置拥塞算法为BBR,无需额外设置,重启即可用;
3、更新至4.9正式版

OpenVZ的就不要试了,不可用

注意,本人编译的内核bbr不是模块化的,通过lsmod|grep bbr是查不到结果的,fq也是。

安装步骤:

wget -O- http://soft.wellphp.com/scripts/install_bbr_centos.sh | bash

安装结束之后,重启:

reboot

重启之后验证是否已经成功:

sysctl net.ipv4.tcp_congestion_control net.core.default_qdisc

结果如下:

net.ipv4.tcp_congestion_control = bbr
net.core.default_qdisc = fq

如果不幸失败,可以通过VNC修改启动项到老的内核。

6步骤实现CentOS系统环境精简优化

随着VPS主机使用的深化,老左从开始使用面板工具操作VPS,再到后来的一键安装包,现在基本上能自学自用VPS管理网站/备份网站维护。但是需要深入的运维还需要时日,比如刚才看到一篇CentOS系统环境精简优化的文章还是值得分享的。但是在操作之前,最好建议在我们安装系统之后操作,而不要在有网站运行之后操作,以免系统出现问题。

随着VPS主机使用的深化,老左从开始使用面板工具操作VPS,再到后来的一键安装包,现在基本上能自学自用VPS管理网站/备份网站维护。但是需要深入的运维还需要时日,比如刚才看到一篇CentOS系统环境精简优化的文章还是值得分享的。但是在操作之前,最好建议在我们安装系统之后操作,而不要在有网站运行之后操作,以免系统出现问题。

第一步、删除不必要的自带软件包

yum remove Deployment_Guide-en-US finger cups-libs cups ypbind
yum remove bluez-libs desktop-file-utils ppp rp-pppoe wireless-tools irda-utils
yum remove sendmail* samba* talk-server finger-server bind* xinetd
yum remove nfs-utils nfs-utils-lib rdate fetchmail eject ksh mkbootdisk mtools
yum remove syslinux tcsh startup-notification talk apmd rmt dump setserial portmap yp-tools
yum groupremove "Mail Server" "Games and Entertainment" "X Window System" "X Software Development"
yum groupremove "Development Libraries" "Dialup Networking Support"
yum groupremove "Games and Entertainment" "Sound and Video" "Graphics" "Editors"
yum groupremove "Text-based Internet" "GNOME Desktop Environment" "GNOME Software Development"

第二步、升级centos系统

yum update #更新系统
yum clean all #清理全部缓存文件

第三步、禁用seLinux

sestatus #先执行看seLinux状态,如果不是disabled,就需要执行下面步骤,否则不要执行
vi /etc/selinux/config
SELINUX=disabled #禁用SeLinux
SELINUX=enforcing #使用SeLinux

第四步、禁止IPV6(执行后需要reboot重启)

vi /etc/modprobe.conf #打开文件,把下面两行加到最后
alias net-pf-10 off
alias ipv6 off

第五步、初始化防火墙

touch /etc/sysconfig/iptables
iptables -F
iptables -X
iptables -Z
service iptables save
service iptables restart

第六步、禁止无用服务

#! /bin/bash
service acpid off
service atd stop
service auditd stop
service avahi-daemon stop
service avahi-dnsconfd stop
service bluetooth stop
service conman stop
service cpuspeed stop
service cups stop
service dnsmasq stop
service dund stop
service firstboot stop
service hidd stop
service httpd stop
service ibmasm stop
service ip6tables stop
service irda stop
service kdump stop
service lm_sensors stop
service mcstrans stop
service messagebus stop
service microcode_ctl stop
service netconsole stop
service netfs stop
service netplugd stop
service nfs stop
service nfslock stop
service nscd stop
service ntpd stop
service oddjobd stop
service pand stop
service pcscd stop
service portmap stop
service psacct stop
service rdisc stop
service restorecond stop
service rpcgssd stop
service rpcidmapd stop
service rpcsvcgssd stop
service saslauthd stop
service sendmail stop
service setroubleshoot stop
service smb stop
service vncserver stop
service winbind stop
service wpa_supplicant stop
service xfs stop
service ypbind stop
service yum-updatesd stop
chkconfig acpid off
chkconfig atd off
chkconfig auditd off
chkconfig avahi-daemon off
chkconfig avahi-dnsconfd off
chkconfig bluetooth off
chkconfig conman off
chkconfig cpuspeed off
chkconfig cups off
chkconfig dnsmasq off
chkconfig dund off
chkconfig firstboot off
chkconfig hidd off
chkconfig httpd off
chkconfig ibmasm off
chkconfig ip6tables off
chkconfig irda off
chkconfig kdump off
chkconfig lm_sensors off
chkconfig mcstrans off
chkconfig messagebus off
chkconfig microcode_ctl off
chkconfig netconsole off
chkconfig netfs off
chkconfig netplugd off
chkconfig nfs off
chkconfig nfslock off
chkconfig nscd off
chkconfig ntpd off
chkconfig oddjobd off
chkconfig pand off
chkconfig pcscd off
chkconfig portmap off
chkconfig psacct off
chkconfig rdisc off
chkconfig restorecond off
chkconfig rpcgssd off
chkconfig rpcidmapd off
chkconfig rpcsvcgssd off
chkconfig saslauthd off
chkconfig sendmail off
chkconfig setroubleshoot off
chkconfig smb off
chkconfig vncserver off
chkconfig winbind off
chkconfig wpa_supplicant off
chkconfig xfs off
chkconfig ypbind off
chkconfig yum-updatesd off

这样通过上述6步骤,就可以完成对centos精简和优化。

openwrt collect ip traffic

#!/bin/sh
# @Create on 2013-12-01

#!/bin/sh

# @Create on 2013-12-01

echo "Collecting data…"

echo ""

cat /proc/net/arp | grep : | grep ^172.16.100 | grep -v 00:00:00:00:00:00| awk ‘{print $1}’> mac-ip

iptables -N UPLOAD

iptables -N DOWNLOAD

while read line;do iptables -I FORWARD 1 -s $line -j UPLOAD;done < mac-ip

while read line;do iptables -I FORWARD 1 -d $line -j DOWNLOAD;done < mac-ip

sleep 10

echo "Download speed:"

echo ""

iptables -nvx -L FORWARD | grep DOWNLOAD | awk ‘{print $2/1024/10" KB/s ",$1/10" packets/s", $9}’ | sort -n -r

echo ""

echo "Upload speed:"

echo ""

iptables -nvx -L FORWARD | grep UPLOAD | awk ‘{print $2/1024/10" KB/s ",$1/10" packets/s", $8}’ | sort -n -r

while read line;do iptables -D FORWARD -s $line -j UPLOAD;done < mac-ip

while read line;do iptables -D FORWARD -d $line -j DOWNLOAD;done < mac-ip

iptables -X UPLOAD

iptables -X DOWNLOAD