download_files

下载文件插件

# coding: utf-8
import platform
import uuid
import os
import re

from framework.plugins import BasePlugin

if os.name == "nt":
    pwd = None
else:
    import pwd


class DownloadFiles(BasePlugin):
    def start(self, params):

        if not params['user']:
            self.send_result(1, 'The user parameter cannot be empty')
            return

        uid = None
        if os.name != 'nt':
            try:
                uid = pwd.getpwnam(params['user']).pw_uid
            except KeyError:
                self.send_result(1, "There are no users in the current system{}".format(params['user']))

        exit_code = 0
        pattern = r"dstPath=(((.*?)&\w+=)|(.*))"
        result = re.search(pattern, params['files']['uri'].decode('utf8'))
        get_result = result.group()
        count = 0
        list1 = []
        for i in get_result:
            if i == '=':
                count += 1
        get_split = get_result.split('=')
        if count > 1:
            get_path = get_split[1]
            for i, v in enumerate(get_path):
                if v == '&':
                    t = i
            for j, v in enumerate(get_path):
                if j < t:
                    list1.append(v)
            target_path = ''.join(list1)
        else:
            target_path = get_split[-1]

        if not os.path.exists(target_path):
            os.makedirs(target_path)

        for value in params['files']:
            cxt = {
                'id': value['id'],
                'filepath': '',
                'success': False,
                'reason': ''
            }
            result = self.fetch_file(value['uri'])
            if result['code'] == 0:
                get_path = os.path.join(target_path, os.path.basename(result['file_path']))
                try:
                    os.rename(result['file_path'], get_path)
                    if os.name.lower() != 'windows':
                        os.chown(get_path, uid, -1)
                    cxt['filepath'] = get_path
                    cxt['success'] = True
                except Exception as e:
                    exit_code += 1
                    cxt['reason'] = str(e)
            else:
                cxt['reason'] = result['error']
                exit_code += 1
            self.send_progress('log', cxt)
        self.send_result(exit_code, "Success" if exit_code == 0 else "Failed")


if __name__ == '__main__':
    from pyramid.node import Node

    rand_id = uuid.uuid1().hex
    plugin_node = Node(rand_id)
    extra_args = {}
    if platform.system().lower() == "linux":
        params = {
            "user": "root",
            "files": [
                {
                    "id": "b837fef2c3794208a1f0901b9bd56d71",
                    "uri": "ufs://id=b837fef2c3794208a1f0901b9bd56d71&filename=111.jpg&md5=7db9dbf4346f7ce916b97bd59ea97f87",
                    "extension": {}
                },
                {
                    "id": "9f96d399f6b74513bb9cc1cf2d591018",
                    "uri": "ufs://id=9f96d399f6b74513bb9cc1cf2d591018&filename=222.jpg&md5=02c9b9acc775eadcc27098f1895500f4",
                    "extension": {}
                },
            ]
        }
    elif platform.system().lower() == "windows":
        params = {
            "user": "root",
            "files": [
                {
                    "id": "b837fef2c3794208a1f0901b9bd56d71",
                    "uri": "ufs://id=9f96d399f6b74513bb9cc1cf2d591018&filename=111.jpg&md5=7db9dbf4346f7ce916b97bd59ea97f87",
                    "extension": {}
                },
                {
                    "id": "af231bd27b0041fc90d54b5f1111dc65",
                    "uri": "ufs://id=9f96d399f6b74513bb9cc1cf2d591018&filename=222.jpg&md5=02c9b9acc775eadcc27098f1895500f4",
                    "extension": {}
                },
            ]
        }
    else:
        print("Other System")
    d = DownloadFiles(plugin_node, extra_args)
    d.start(params)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值