django+javascrpt+python實現私有云盤程式碼
阿新 • • 發佈:2018-12-17
1 # -*- coding: utf-8 -*- 2 #!/usr/bin/python2.7 3 import sys,json,random,os,time,redis,hashlib 4 from django.forms.models import model_to_dict 5 from django.contrib.auth.decorators import login_required 6 from django.views.decorators.csrf import csrf_exempt 7 from django.shortcuts importrender,render_to_response,redirect,HttpResponse 8 from django.http import StreamingHttpResponse 9 from django.http import FileResponse 10 from django.db.models import Q 11 from project.models import UserInfo,TeamInfo,OneFile,TeamFile,CompanyFile 12 13 def user_auth(login_user): 14 check_user_status = UserInfo.objects.filter(user_name=login_user)15 if len(check_user_status) != 0: 16 return "auth_success" 17 else: 18 return "auth_failed" 19 20 def recv_username_redis(ran_id): 21 r = redis.Redis(host='127.0.0.1', port=6379) 22 user_name = r.get(ran_id) 23 return user_name 24 25 #建立目錄在這裡進行 26 @csrf_exempt27 def ftp(request): 28 is_login = request.session.get('IS_LOGIN',False) 29 ran_id = request.session.get('random',False) 30 if is_login: 31 login_user = recv_username_redis(ran_id) 32 result = user_auth(login_user) 33 if result == "auth_success": 34 pass 35 elif result == "auth_failed": 36 return redirect('/login') 37 38 if request.method == "POST": 39 username = recv_username_redis(ran_id) 40 select_type = request.POST.get("type") 41 if select_type == "recv_username_filelist": #獲取個人相關的檔案資訊,涉及個人、部門、公司 42 recv_team_id = UserInfo.objects.filter(user_name=username) 43 for obj in recv_team_id: 44 team_id = obj.team_id 45 46 recv_one_file = OneFile.objects.filter(user_id=username).order_by("-lasttime") 47 recv_team_file = TeamFile.objects.filter(team_id=team_id).order_by("-lasttime") 48 recv_company_file = CompanyFile.objects.order_by("own_user").order_by("-lasttime") 49 temp_num1 = 0 50 temp_dic1 = {} 51 Data = {} 52 for obj in recv_one_file: 53 if os.path.exists(obj.onefile_dir): 54 temp_dic1[temp_num1] = [obj.onefile_name,obj.file_size,obj.datatime,obj.lasttime,obj.user_id,obj.flage,obj.onefile_share,obj.onefile_dir] 55 temp_num1 += 1 56 else: 57 pass 58 Data["recv_one_file"] = temp_dic1 59 60 temp_num2 = 0 61 temp_dic2 = {} 62 for obj in recv_team_file: 63 if os.path.exists(obj.teamfile_dir): 64 temp_dic2[temp_num2] = [obj.teamfile_name,obj.file_size,obj.datatime,obj.lasttime,obj.own_user,obj.flage,obj.teamfile_dir] 65 temp_num2 += 1 66 else: 67 pass 68 Data["recv_team_file"] = temp_dic2 69 70 temp_num3 = 0 71 temp_dic3 = {} 72 for obj in recv_company_file: 73 if os.path.exists(obj.companyfile_dir): 74 temp_dic3[temp_num3] = [obj.companyfile_name,obj.file_size,obj.datatime,obj.lasttime,obj.own_user,obj.flage,obj.companyfile_dir] 75 temp_num3 += 1 76 else: 77 pass 78 Data["recv_company_file"] = temp_dic3 79 return HttpResponse(json.dumps(Data)) 80 elif select_type == "recv_recycle_filelist": 81 #後期如果需要新增徹底刪除模組,則將對應檔案的狀態修改為disable,這裡的檔案顯示則只顯示狀態為enable的就可以,預設檔案儲存在資料庫中不刪除,同時定期備份的話只需要將FTP和del_FTP兩個目錄整體打包備份即可。 82 recv_team_id = UserInfo.objects.filter(user_name=username) 83 for obj in recv_team_id: 84 team_id = obj.team_id 85 recv_one_file = OneFile.objects.filter(user_id=username).order_by("-lasttime") 86 recv_team_file = TeamFile.objects.filter(team_id=team_id).order_by("-lasttime") 87 recv_company_file = CompanyFile.objects.order_by("own_user").order_by("-lasttime") 88 temp_num = 0 89 temp_dic = {} 90 Data = {} 91 for obj in recv_one_file: 92 if os.path.exists(obj.onefile_del_dir): 93 temp_dic[temp_num] = [obj.onefile_name,obj.file_size,obj.datatime,obj.lasttime,obj.user_id,obj.flage,obj.onefile_del_dir] 94 temp_num += 1 95 else: 96 pass 97 for obj in recv_team_file: 98 if os.path.exists(obj.teamfile_del_dir): 99 temp_dic[temp_num] = [obj.teamfile_name,obj.file_size,obj.datatime,obj.lasttime,obj.own_user,obj.flage,obj.teamfile_del_dir] 100 temp_num += 1 101 else: 102 pass 103 for obj in recv_company_file: 104 if os.path.exists(obj.companyfile_del_dir): 105 temp_dic[temp_num] = [obj.companyfile_name,obj.file_size,obj.datatime,obj.lasttime,obj.own_user,obj.flage,obj.companyfile_del_dir] 106 temp_num += 1 107 else: 108 pass 109 return HttpResponse(json.dumps(temp_dic)) 110 elif select_type == "recv_share_list_info": #獲取共享檔案列表資訊 111 Data = {} 112 recv_share_flage = OneFile.objects.filter(user_id=username) 113 r = redis.Redis(host='127.0.0.1', port=6379) 114 for obj in recv_share_flage: 115 temp_dic = {} 116 if len(obj.onefile_share) != 0: 117 temp_dic["file_name"] = obj.onefile_name 118 temp_dic["file_path"] = obj.onefile_dir 119 temp_dic["file_size"] = obj.file_size 120 get_key = json.loads(r.get(obj.onefile_share)) 121 temp_dic["get_url"] = "http://192.168.12.11/get_auth?"+obj.onefile_share 122 temp_dic["get_random"] = get_key["random"] 123 TTL = r.ttl(obj.onefile_share)/3600/24 124 temp_dic["get_ttl"] = TTL 125 Data[obj.onefile_dir] = temp_dic 126 else: 127 pass 128 if len(Data) == 0: 129 return HttpResponse(json.dumps("failed")) 130 else: 131 return HttpResponse(json.dumps(Data)) 132 elif select_type == "select_one_file": #檔案查詢 133 file_name = request.POST.get("file_name") 134 recv_one_file = OneFile.objects.filter(Q(onefile_name__icontains=file_name,user_id=username)) 135 temp_dic = {} 136 temp_num = 0 137 for obj in recv_one_file: 138 if os.path.exists(obj.onefile_dir): 139 temp_dic[temp_num] = [obj.onefile_name,obj.file_size,obj.datatime,obj.lasttime,obj.user_id,obj.flage,obj.onefile_share,obj.onefile_dir] 140 temp_num += 1 141 else: 142 pass 143 if len(temp_dic) == 0: 144 return HttpResponse(json.dumps("failed")) 145 else: 146 return HttpResponse(json.dumps(temp_dic)) 147 elif select_type == "select_team_file": #部門檔案查詢 148 file_name = request.POST.get("file_name") 149 recv_team_id = UserInfo.objects.filter(user_name=username) 150 for obj in recv_team_id: 151 team_id = obj.team_id 152 153 recv_team_file = TeamFile.objects.filter(Q(teamfile_name__icontains=file_name,team_id=team_id)) 154 temp_dic = {} 155 temp_num = 0 156 for obj in recv_team_file: 157 if os.path.exists(obj.teamfile_dir): 158 temp_dic[temp_num] = [obj.teamfile_name,obj.file_size,obj.datatime,obj.lasttime,obj.own_user,obj.flage,obj.teamfile_dir] 159 temp_num += 1 160 else: 161 pass 162 if len(temp_dic) == 0: 163 return HttpResponse(json.dumps("failed")) 164 else: 165 return HttpResponse(json.dumps(temp_dic)) 166 else: 167 return render_to_response("ftp.html") 168 else: 169 return redirect('/login') 170 171 #檔案對應碼,使用隨機生成,和檔案明對應。 172 def generate_verification_code(): 173 ''' 隨機生成6位的驗證碼 ''' 174 code_list = [] 175 for i in range(10): # 0-9數字 176 code_list.append(str(i)) 177 for i in range(65, 91): # A-Z 178 code_list.append(chr(i)) 179 for i in range(97, 123): # a-z 180 code_list.append(chr(i)) 181 182 myslice = random.sample(code_list, 6) # 從list中隨機獲取6個元素,作為一個片斷返回 183 verification_code = ''.join(myslice) # list to string 184 return verification_code 185 186 #共享檔案,生成共享碼儲存到redis,展示到前端 187 @csrf_exempt 188 def share_one_file(request): 189 is_login = request.session.get('IS_LOGIN',False) 190 ran_id = request.session.get('random',False) 191 if is_login: 192 login_user = recv_username_redis(ran_id) 193 result = user_auth(login_user) 194 if result == "auth_success": 195 pass 196 elif result == "auth_failed": 197 return redirect('/login') 198 199 if request.method == "POST": 200 # share_url = request.POST.get("url") 201 select_type = request.POST.get("type") 202 if select_type == "share_one_file": #共享一個檔案 203 file_name = request.POST.get("file_name") 204 share_path = request.POST.get("share_path") 205 Random6 = generate_verification_code() 206 207 get_file_path = OneFile.objects.filter(onefile_name=file_name,onefile_dir=share_path) 208 for obj in get_file_path: 209 file_path = obj.onefile_dir 210 211 get_team_id = UserInfo.objects.filter(user_name=login_user) 212 for obj in get_team_id: 213 team_id = obj.team_id 214 215 r = redis.Redis(host='127.0.0.1', port=6379) 217 share_url = "http://192.168.12.11:8080/get_auth?file_name="+file_name+"&type=auth_get" 218 Data = json.dumps({"url":share_url,"random":Random6,"user":login_user}) 219 220 file_path_join = file_path+"_"+login_user 221 set_result = r.set(hashlib.sha256(file_path_join.encode("utf-8")).hexdigest(),Data,ex=864000,nx=True) 222 if set_result: 223 get_info = {"url":"http://192.168.12.11:8080/get_auth?"+hashlib.sha256(file_path_join.encode("utf-8")).hexdigest(),"random":Random6} 224 recv_data = json.dumps({"status":"success","info":get_info,"ttl":10}) #10天 225 get_file_path.update(onefile_share=hashlib.sha256(file_path_join.encode("utf-8")).hexdigest()) 226 else: 227 Random = json.loads(r.get(hashlib.sha256(file_path_join.encode("utf-8")).hexdigest()))["random"] 228 get_info = {"url":"http://192.168.12.11:8080/get_auth?"+hashlib.sha256(file_path_join.encode("utf-8")).hexdigest(),"random":Random} 229 TTL = r.ttl(hashlib.sha256(file_path_join.encode("utf-8")).hexdigest())/3600/24 230 recv_data = json.dumps({"status":"exist","info":get_info,"ttl":TTL}) #TTL轉換成天 231 232 return HttpResponse(recv_data) 233 elif select_type == "del_share_one_file": #取消一個檔案共享 234 file_name = request.POST.get("file_name") 235 file_path = request.POST.get("file_path") 236 get_file_path = OneFile.objects.filter(onefile_name=file_name,onefile_dir=file_path) 237 for obj in get_file_path: 238 file_share = obj.onefile_share 239 get_file_path.update(onefile_share="") 240 r = redis.Redis(host='127.0.0.1', port=6379) 241 r.delete(file_share) 242 get_share = r.get(file_share) 243 if get_share != None: 244 return HttpResponse(json.dumps({"status":"failed"})) 245 else: 246 return HttpResponse(json.dumps({"status":"success"})) 247 elif select_type == "del_one_file": #刪除一個檔案 248 file_name = request.POST.get("file_name") 249 file_path = request.POST.get("file_path") 250 recv_file_path = OneFile.objects.filter(onefile_name=file_name,onefile_dir=file_path) 251 for obj in recv_file_path: 252 source_path = obj.onefile_dir 253 desc_path = obj.onefile_del_dir 254 share_flage = obj.onefile_share 255 if len(share_flage) != 0: 256 return HttpResponse(json.dumps({"status":"share"})) 257 else: 258 os.system("mv "+source_path+" "+desc_path) 259 if os.path.exists(source_path): 260 return HttpResponse(json.dumps({"status":"failed"})) 261 else: 262 return HttpResponse(json.dumps({"status":"success"})) 263 elif select_type == "del_team_file": #刪除一個部門檔案 264 file_name = request.POST.get("file_name") 265 file_path = request.POST.get("file_path").split(":")[0] 266 recv_file_path = TeamFile.objects.filter(teamfile_name=file_name,teamfile_dir=file_path) 267 for obj in recv_file_path: 268 source_path = obj.teamfile_dir 269 desc_path = obj.teamfile_del_dir 270 file_user = obj.own_user 271 if login_user != file_user: 272 return HttpResponse(json.dumps({"status":"user_err"})) 273 else: 274 os.system("mv "+source_path+" "+desc_path) 275 if os.path.exists(source_path): 276 return HttpResponse(json.dumps({"status":"failed"})) 277 else: 278 return HttpResponse(json.dumps({"status":"success"})) 279 elif select_type == "Restoring_files": #還原一個被刪除的檔案 280 file_name = request.POST.get("file_name") 281 restor_path = request.POST.get("restor_path") #回收站中的地址 282 desc_path = ("").join(restor_path.split("_del")) #需要還原回去的地址 283 status = os.system("mv "+restor_path+" "+desc_path) 284 if os.path.exists(desc_path): 285 return HttpResponse(json.dumps({"status":"success"})) 286 else: 287 return HttpResponse(json.dumps({"status":"failed"})) 288 289 else: 290 return render_to_response("share_file.html") 291 else: 292 return redirect('/login') 293 294 295 #個人檔案上傳 296 @csrf_exempt 297 def file_put_one(request): 298 is_login = request.session.get('IS_LOGIN',False) 299 ran_id = request.session.get('random',False) 300 if is_login: 301 login_user = recv_username_redis(ran_id) 302 result = user_auth(login_user) 303 if result == "auth_success": 304 pass 305 elif result == "auth_failed": 306 return redirect('/login') 307 308 if request.method == "POST": 309 username = recv_username_redis(ran_id) 310 Data = request.FILES.get("one_navigation_bar_put") 311 312 if Data: 313 one_file_random = str(random.randint(0,10000)%999) #使用者預設上傳檔案的名稱,和上傳檔案的真實名稱做關聯 314 one_file_path = UserInfo.objects.filter(user_name=username) 315 for obj in one_file_path: 316 one_dir_path = obj.user_path 317 one_del_dir_path = obj.user_path_del 318 319 save_file_name = one_dir_path+"/"+one_file_random 320 save_del_file_name = one_del_dir_path+"/"+one_file_random 321 #save_file_name = "/opt/Django/auto_ops/111" 322 file_name = Data.name 323 file_size = Data.size 324 325 check_file_exist_sql = OneFile.objects.filter(onefile_name=file_name,user_id=username) 326 for obj in check_file_exist_sql: 327 if os.path.exists(obj.onefile_dir): 328 return HttpResponse("file_exist") 329 else: 330 pass 331 # if len(check_file_exist_sql) != 0: 332 # return HttpResponse("file_exist") 333 # else: 334 # pass 335 336 with open(save_file_name,"wb") as new_file: 337 for chunk in Data.chunks(): 338 new_file.write(chunk) 339 new_file.close() 340 341 if os.path.exists(save_file_name): 342 if os.path.isfile(save_file_name): 343 file_type = "file" 344 elif os.path.isdir(save_file_name): 345 file_type = "dir" 346 347 check_file_size = os.path.getsize(save_file_name) 348 Time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) 349 if int(check_file_size) == int(file_size): 350 file_size = file_size/float(1024*1024) 351 file_size = str(round(file_size,2))+"MB" 352 353 save_file = OneFile.objects.create(user_id=username,onefile_dir=save_file_name,onefile_dir_all_list=json.dumps([username]),onefile_name=file_name,onefile_del_dir=save_del_file_name,onefile_del_dir_all_list=json.dumps([username]),onefile_del_name=file_name,datatime=Time,lasttime=Time,file_type=file_type,flage='