download_files

公孙栋
2023-12-01

下载文件插件

# 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)
 类似资料:

相关阅读

相关文章

相关问答