def newOrEdit(request): base_url = 'media/' + str(request.session['id']) + '/' if request.method == 'POST': id = request.POST.get('id') name = request.POST.get('name') content = request.POST.get('content') dir = request.POST.get("dir") date = time.strftime('%Y-%m-%d', time.localtime(time.time())) db = MysqlHelper() sql = "SELECT * FROM sysite_file WHERE id = %s" params = [id] res = db.fetchone(sql, params) print(res) url = base_url + name + '.txt' # 编辑则重新写入文件,新建则新建文件 with open(base_url + name + '.txt', 'w+') as f: f.write(content) size = getFileSize(base_url + name + '.txt') print(size) if res: # 编辑原有文件,需要改变大小 db = MysqlHelper() sql3 = "UPDATE sysite_file SET name = %s, size = %s WHERE id = %s" db.update(sql3, [name, size, id]) data = {"result": "success", "message": "编辑成功"} else: # 新建txt文件 db = MysqlHelper() sql1 = "INSERT INTO sysite_file (name, type, author, time, size, url) VALUES (%s, %s, %s, %s, %s, %s)" params = [name, "txt", request.session['id'], date, size, url] res = db.insert(sql1, params) db = MysqlHelper() sql3 = "SELECT max(id) from sysite_file" new_id = db.fetchone(sql3, [])[0] # 插入关联表 db = MysqlHelper() sql2 = "INSERT INTO sysite_fileuser (userId, fileId, dir, oDate) VALUES (%s, %s, %s, %s)" params = [request.session['id'], new_id, dir, date] res2 = db.insert(sql2, params) print(res2) data = {"result": "success", "message": "新建成功"} return HttpResponse(json.dumps(data, cls=JSONEncoder), content_type='application/json')
def email_confirm(request): if request.method == 'POST': # tag = models.User.objects.filter(mailbox=request.POST.get('email')) mailbox = request.POST.get('email') db = MysqlHelper() sql = "SELECT * FROM sysite_user WHERE mailbox = %s" params = [mailbox] res = db.fetchone(sql, params) if res: data = 'error' else: data = 'success' return HttpResponse(json.dumps(data, cls=JSONEncoder), content_type='application/json')
def id_confirm(request): if request.method == 'POST': name = request.POST.get('username') # tag = models.User.objects.filter(id=request.POST.get('username')) db = MysqlHelper() sql = "SELECT * FROM sysite_user WHERE name = %s" params = [name] res = db.fetchone(sql, params) print(res) if res: data = 'error' else: data = 'success' return HttpResponse(json.dumps(data, cls=JSONEncoder), content_type='application/json')
def login(request): if request.method == 'GET': return render(request, 'static/html/login.html') else: username = request.POST.get('username') password = request.POST.get('password') db = MysqlHelper() sql = "SELECT * FROM sysite_user WHERE name = %s and password = %s" params = [username, password] res = db.fetchone(sql, params) print(res) if res: data = {'result': 'success', 'level': res[5]} request.session['id'] = res[0] request.session['name'] = res[3] request.session['level'] = res[5] else: data = "error" return HttpResponse(json.dumps(data, cls=JSONEncoder), content_type='application/json')
def paste(request): base_url = 'media/' + str(request.session['id']) + '/' if request.method == 'POST': type = request.POST.get('type') id = request.POST.get('id') name = request.POST.get('name') dir = request.POST.get('dir') size = request.POST.get('size') url = base_url + name + '.' + type copyUrl = base_url + name + '-copy.' + type date = time.strftime('%Y-%m-%d', time.localtime(time.time())) if type != 'folder': db = MysqlHelper() sql1 = "INSERT INTO sysite_file (name, type, author, time, size, url) VALUES (%s, %s, %s, %s, %s, %s)" params = [name + '-copy', type, request.session['id'], date, size, copyUrl] res = db.insert(sql1, params) db = MysqlHelper() sql3 = "SELECT max(id) from sysite_file" new_id = db.fetchone(sql3, [])[0] # 插入关联表 db = MysqlHelper() sql2 = "INSERT INTO sysite_fileuser (userId, fileId, dir, oDate) VALUES (%s, %s, %s, %s)" params = [request.session['id'], new_id, dir, date] res2 = db.insert(sql2, params) print(res2) # 复制并重命名新文件 shutil.copy(url, copyUrl) if res and res2: data = {"result": "success", "message": "成功粘贴"} else: data = {"result": "failed"} else: # 文件夹剪切 db = MysqlHelper() sql2 = "UPDATE sysite_folderuser SET dir = %s WHERE userId = %s, folderId = %s" res = db.update(sql2, [dir, request.session['id'], id]) if res: data = {"result": "success", "message": "剪切成功"} else: data = {"result": "failed"} return HttpResponse(json.dumps(data, cls=JSONEncoder), content_type='application/json')
def upload(request): base_url = 'media/' + str(request.session['id']) + '/' if not os.path.exists(base_url): os.makedirs(base_url) if request.method == 'POST': file = request.FILES.get('file') # 获取文件对象,包括文件名文件大小和文件内容 dir = request.POST.get('dir') print(dir) fileName = file.name # 正则表达式获取前缀 prefix = re.findall(r'(.+?)\.', fileName)[0] # type = re.findall(r'\.[^.\\/:*?"<>|\r\n]+$', fileName)[0] # python获取后缀 type = os.path.splitext(fileName)[-1][1:] date = time.strftime('%Y-%m-%d', time.localtime(time.time())) # 写入文件 with open(base_url + file.name, 'wb+') as f: for chunk in file.chunks(): f.write(chunk) size = formatSize(file.size) print(size) db = MysqlHelper() sql1 = "INSERT INTO sysite_file (name, type, author, time, size, url) VALUES (%s, %s, %s, %s, %s, %s)" params = [prefix, type, request.session['id'], date, size, base_url+file.name] res = db.insert(sql1, params) db = MysqlHelper() sql3 = "SELECT max(id) from sysite_file" new_id = db.fetchone(sql3, [])[0] # 插入关联表 db = MysqlHelper() sql2 = "INSERT INTO sysite_fileuser (userId, fileId, dir, oDate) VALUES (%s, %s, %s, %s)" params = [request.session['id'], new_id, dir, date] res2 = db.insert(sql2, params) print(res2) data = { "result": "success", "detail": {"name": prefix, "type": type, "owner": request.session['id'], "time": date, "size": file.size}} return HttpResponse(json.dumps(data, cls=JSONEncoder), content_type='application/json')
def newFolder(request): if request.method == 'POST': name = request.POST.get('name') type = request.POST.get('type') # type = 'folder' owner = request.POST.get('owner') dir = request.POST.get('parent_dir') date = time.strftime('%Y-%m-%d', time.localtime(time.time())) db = MysqlHelper() sql1 = "INSERT INTO sysite_folder (name, author, time) VALUES (%s, %s, %s)" params = [name, request.session['id'], date] res = db.insert(sql1, params) db = MysqlHelper() sql3 = "SELECT max(id) from sysite_folder" new_id = db.fetchone(sql3, [])[0] # 插入关联表 db = MysqlHelper() sql2 = "INSERT INTO sysite_folderuser (userId, folderId, dir, oDate) VALUES (%s, %s, %s, %s)" params = [request.session['id'], new_id, dir, date] res2 = db.insert(sql2, params) print(res2) # models.Folder.objects.create(name=name, type=type, owner=request.session['id'], time=date, parent_dir=parent_dir) data = {"result": "success", "detail": {"name": name, "type": type, "owner": request.session['id'], "time": date}} return HttpResponse(json.dumps(data, cls=JSONEncoder), content_type='application/json')