Esempio n. 1
0
 def upload(self):
     try:
         bundle_id = request.form['bundleId']
         file = request.files['Filedata']
         filename = str(uuid.uuid1()) + "." + file.filename.rsplit('.', 1)[1]
         if file and self.allowed_file(filename):
             ext = filename.split('.')[-1]
             name = str(time.time()) + '_' + filename
             tmp_file = os.path.join(settings['tmp_dir'], name)
             file.save(tmp_file)
             sha1 = sha1_of_file(tmp_file)
             pic_path = hash_to_path(sha1) + '.' +ext
             pic_path = os.path.join(settings['pic_upload_dir'], pic_path)
             dir_path = os.path.dirname(pic_path)
             if not os.path.isdir(dir_path):
                 os.makedirs(dir_path)
             shutil.move(tmp_file, pic_path)
             pic_url = '#IMG_HOST#%s.%s' % (hash_to_path(sha1), ext)
             if request.form['device'] == 'iphone':
                 data = {'screenshotUrls': pic_url}
             if request.form['device'] == 'ipad':
                 data = {'ipadScreenshotUrls': pic_url}
             if request.form["lang"] == "en":
                 DB.AppBase.update({'bundleId': bundle_id}, {'$addToSet': data})
             else:
                 try:
                     DB.AppBase_CN.update({'bundleId': bundle_id}, {'$addToSet': data}, upsert=True)
                 except Exception:
                     data = data.update({'bundleId': bundle_id})
                     DB.AppBase_CN.insert(data)
             status, message = 'success', u'上传成功'
         else:
             raise Exception(u'必须是jpg,png文件')
     except Exception, ex:
         status, message = 'error', u'上传失败:' + str(ex)
def upload_hash_file(content, filename, target_dir, allow_ext=['png', 'jpg', 'jpeg']):
    '''
    上传文件
    '''
    if file:
        filename = filename.lower()
        filename = str(uuid.uuid1()) + "." + filename.rsplit('.', 1)[1]
        tmpfile = os.path.join(settings['tmp_dir'], filename)
        with open(tmpfile, "w") as tt: tt.write(content)
        hash_str = sha1_of_file(tmpfile)
        save_file = '%s.%s' % (hash_to_path(hash_str), filename.rsplit('.', 1)[1])
        abs_save_file = os.path.join(target_dir, save_file)
        save_file_dir = os.path.dirname(abs_save_file)
        if not os.path.exists(save_file_dir): os.makedirs(save_file_dir)
        shutil.move(tmpfile, abs_save_file)
    else:
        raise Exception('上传失败')
    return hash_str, abs_save_file, save_file
Esempio n. 3
0
def upload_hash_file(file, target_dir, allow_ext=['png', 'jpg', 'jpeg']):
    '''
    上传文件
    '''
    if file:
        #filename = secure_filename(file.filename)
        filename = file.filename.lower()
        if '.' not in filename or filename.rsplit('.', 1)[1] not in set(allow_ext):
            raise Exception('文件类型不允许上传')
        filename = str(uuid.uuid1()) + "." + filename.rsplit('.', 1)[1]
        tmpfile = os.path.join(settings['tmp_dir'], filename)
        file.save(tmpfile)
        hash_str = sha1_of_file(tmpfile)
        save_file = '%s.%s' % (hash_to_path(hash_str), filename.rsplit('.', 1)[1])
        abs_save_file = os.path.join(target_dir, save_file)
        save_file_dir = os.path.dirname(abs_save_file)
        if not os.path.exists(save_file_dir):
            os.makedirs(save_file_dir)
        shutil.move(tmpfile, abs_save_file)
    else:
        raise Exception('上传失败')

    return hash_str, abs_save_file, save_file
Esempio n. 4
0
                DB.AppBase_CN.update({"_id": app_cn["_id"]}, {"$set": data_cn})
            else:
                data_cn = {
                    "trackId": int(request.form["trackId"]),
                    "trackName": request.form["trackName_CN"],
                    "description": request.form["description_cn"],
                    "releaseNotes": request.form["releaseNotes_cn"]
                }
                DB.AppBase_CN.insert(data_cn)
            file = request.files["pic"]
            if file.filename != "":
                ext = file.filename.split('.')[-1]
                name = str(time.time()) + '_' + str(random.randint(1000000, 9000000))
                tmp_file = os.path.join(settings['tmp_dir'], name)
                file.save(tmp_file)
                sha1 = sha1_of_file(tmp_file)
                pic_path = hash_to_path(sha1) + ".114x114-75." + ext
                full_pic_path = os.path.join(settings['pic_upload_dir'], pic_path)
                dir_path = os.path.dirname(full_pic_path)
                if not os.path.isdir(dir_path):os.makedirs(dir_path)
                shutil.move(tmp_file, full_pic_path)
                pic_url = settings['pic_url_host'] + '/%s' % (hash_to_path(sha1) + "." + ext)
                data["artworkUrl512"] = pic_url
            DB.AppBase.update({'_id':self.app_data['_id']}, {'$set':data})
            message = {'status':'success', 'message':'修改成功'} 
        else:
            message = {'status':'error', 'message':'修改失败'} 

        self._form.add_message(**message)

        return self._view.render('app_edit', form=self._form, app=self.app_data, icon=self.icon)