# Path: library/utils/path.py
# def make_dir(directory, chmods=755 , force=True , backup=True) :
#     '''
#     用于创建目录
#     :parm
#         directory:创建目录
#         chmods：权限，必须是数字，例如755
#         force：当目录存在，强制执行
#         backup：当目录存在，备份文件，后缀名为%Y%m%d%H%M%S-bk
#     :return
#         True：成功
#         Flase：失败
#     '''
#     if os.path.exists(directory) :
#         if not force :
#             return False
#         
#         dt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
#         if backup :
#             try : 
#                 pre_result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 pre_result = str(e)
#         else : 
#             pre_result = False
#             '''
#             #result = os.removedirs(dir)
#             try : 
#                 result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 result = str(e)
#             '''
#     else :
#         pre_result = False
#     
#     if not pre_result :
#         try :
#             chmods = int(chmods)
#         except :
#             chmods = 755
#         
#         try :
#             result = os.makedirs(directory, chmods, exist_ok=True)
#         except Exception as e :
#             result = str(e)
#     else :
#         result = False
#         
#     if not result :
#         result = True
#     else :
#         result = False
#         
#     return result
# 
# Path: library/utils/type_conv.py
# def bytes2string(data, encoding='utf-8'):
#     
#     '''
#     简单粗暴地把bytes字符串转换为str字符串
#     data必须是简单bytes格式
#     如果复杂，转化可能成功，但后续处理出差几率很大，请使用obj2string方法
#     :parm
#         data：字符串
#         encoding：编码
#     '''
#         
#     if isinstance(data, str):
#         return data
#     
#     try :
#         data = data.decode(encoding)
#         # result.decode('ascii') bytes解码会得到str
#     except :
#         new_data = obj2string(data, encoding=encoding)
#         if new_data[0] :
#             data = new_data[1]
#         
#     return data
# 
# Path: library/utils/type_conv.py
# def random_str(ranlen=8):
#     strings = ''
#     chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
#     length = len(chars) - 1
#     random = Random()
#     for i in range(ranlen):
#         i = i
#         strings = strings + chars[random.randint(0, length)]
#     return strings
# 
# Path: lykchat/settings.py
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


import os, time, stat, subprocess, re, filetype
from library.utils.path import make_dir
from library.utils.type_conv import bytes2string
from library.utils.type_conv import random_str
from lykchat.settings import BASE_DIR
    from library.utils.syscmd import os_popen
        
    return (False, '临时文件无法写入/dev/shm/lykops/')


def upload_file(file, upload_dir='', filename='', max_upload_size=1024 * 1024, chmods='444', force=False):
    
    '''
    用于web页面上传文件
    :parm
        file：上传文件内容
        upload_file：上传目录，默认：在项目根目录下的/file/upload
        filename：上传后的文件名，默认：10个随机字符+_+文件名
        max_upload_size：最大上传文件大小，默认为1M
    :return
        False：上传失败
        filename：上传成功后的文件名
    '''
    
    try :
        if file.size > max_upload_size:
            return (False, '上传文件超过最大值')
        
        if not filename :
            filename = random_str(ranlen=10) + '_' + file.name
    except Exception as e:
        return (False, '可能是参数file出问题，' + str(e))
    
    if upload_dir :
        result = path_isexists(upload_dir)
        if not result[0] :

# Path: library/utils/path.py
# def make_dir(directory, chmods=755 , force=True , backup=True) :
#     '''
#     用于创建目录
#     :parm
#         directory:创建目录
#         chmods：权限，必须是数字，例如755
#         force：当目录存在，强制执行
#         backup：当目录存在，备份文件，后缀名为%Y%m%d%H%M%S-bk
#     :return
#         True：成功
#         Flase：失败
#     '''
#     if os.path.exists(directory) :
#         if not force :
#             return False
#         
#         dt = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))
#         if backup :
#             try : 
#                 pre_result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 pre_result = str(e)
#         else : 
#             pre_result = False
#             '''
#             #result = os.removedirs(dir)
#             try : 
#                 result = os.renames(directory , directory + '-' + str(dt) + '-' + 'bk')
#             except Exception as e :
#                 result = str(e)
#             '''
#     else :
#         pre_result = False
#     
#     if not pre_result :
#         try :
#             chmods = int(chmods)
#         except :
#             chmods = 755
#         
#         try :
#             result = os.makedirs(directory, chmods, exist_ok=True)
#         except Exception as e :
#             result = str(e)
#     else :
#         result = False
#         
#     if not result :
#         result = True
#     else :
#         result = False
#         
#     return result
# 
# Path: library/utils/type_conv.py
# def bytes2string(data, encoding='utf-8'):
#     
#     '''
#     简单粗暴地把bytes字符串转换为str字符串
#     data必须是简单bytes格式
#     如果复杂，转化可能成功，但后续处理出差几率很大，请使用obj2string方法
#     :parm
#         data：字符串
#         encoding：编码
#     '''
#         
#     if isinstance(data, str):
#         return data
#     
#     try :
#         data = data.decode(encoding)
#         # result.decode('ascii') bytes解码会得到str
#     except :
#         new_data = obj2string(data, encoding=encoding)
#         if new_data[0] :
#             data = new_data[1]
#         
#     return data
# 
# Path: library/utils/type_conv.py
# def random_str(ranlen=8):
#     strings = ''
#     chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
#     length = len(chars) - 1
#     random = Random()
#     for i in range(ranlen):
#         i = i
#         strings = strings + chars[random.randint(0, length)]
#     return strings
# 
# Path: lykchat/settings.py
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Path: library/utils/file.py
import os, time, stat, subprocess, re, filetype
from library.utils.path import make_dir
from library.utils.type_conv import bytes2string
from library.utils.type_conv import random_str
from lykchat.settings import BASE_DIR
    from library.utils.syscmd import os_popen

        
    return (False, '临时文件无法写入/dev/shm/lykops/')


def upload_file(file, upload_dir='', filename='', max_upload_size=1024 * 1024, chmods='444', force=False):
    
    '''
    用于web页面上传文件
    :parm
        file：上传文件内容
        upload_file：上传目录，默认：在项目根目录下的/file/upload
        filename：上传后的文件名，默认：10个随机字符+_+文件名
        max_upload_size：最大上传文件大小，默认为1M
    :return
        False：上传失败
        filename：上传成功后的文件名
    '''
    
    try :
        if file.size > max_upload_size:
            return (False, '上传文件超过最大值')
        
        if not filename :
            filename = random_str(ranlen=10) + '_' + file.name
    except Exception as e:
        return (False, '可能是参数file出问题，' + str(e))
    
    if upload_dir :
        result = path_isexists(upload_dir)
        if not result[0] :

            upload_dir = os.path.join(BASE_DIR, 'file/upload/')