#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    demeter web page
    name:upload.py
    author:rabin
"""
from __load__ import *
from datetime import *
import os
import uuid

class index_path(Load):
	"""
	@api {post} /upload 上传图片
	@apiVersion 1.0.0
	@apiName upload
	@apiGroup Common

	@apiParam {String} file 文件流名称

	@apiSuccess {Bool} status 状态
	@apiSuccess {String}   msg  描述
	@apiSuccess {Number}   code  状态码
	@apiSuccess {Object[]} data 返回数据
	@apiSuccess {String}   data.src  图片地址

	@apiErrorExample 操作成功
	{"status": 1, "msg": "操作成功", "code": 0, "data": {"src":"url"}}
	@apiErrorExample 操作失败
	{"status": 2, "msg": "\u64cd\u4f5c\u5931\u8d25", "code": 0, "data": {}}
	"""
 	@auth
	def post(self, *args, **kwargs):
		url = self.request.protocol + "://" + self.request.host
		file_metas = self.request.files["file"]

		# print(file_metas)
		day = str(date.today())
		day = day.split('-')
		for meta in file_metas:
			#meta['filename']
			file_name =  str(uuid.uuid5(uuid.uuid1(), 'file'))
			file_path = day[0] + '/' + day[1] + '/' + day[2]
			file_path = File.mkdirs(os.path.join(Demeter.path, 'runtime','upload', file_path)) + '/' + Demeter.md5(file_name) + '.jpg'
			with open(file_path, 'wb') as up:
				up.write(meta['body'])
		self.out('yes', {'src':url + file_path.replace(Demeter.path + 'runtime', '')})