Esempio n. 1
0
def file_upload(request):
	responseData = {}
	
	url = C.getProtocol(request) + request.get_host()

	reqfile  = request.FILES['upfile']
	suffix   = os.path.splitext(reqfile.name)
	suffix   = str(suffix[1].replace('.', '')).lower()
	
	img      = Image.open(reqfile)

	img.thumbnail((800, 800), Image.ANTIALIAS)
	
	fileName = str(time.time()).replace('.', '') + str(random.randint(100, 9999))

	m2 = hashlib.md5() 
	
	m2.update(fileName)
	
	fileName = str(m2.hexdigest() + '.' + suffix)
	
	fileName = fileName[10:]

	savepath = '/static/upload/' + time.strftime('%Y/%m/',time.localtime(time.time()))
	
	filePath = os.path.dirname(__file__) + savepath
	
	if os.path.exists(filePath) == False:
		os.makedirs(filePath)
	
	filePath = filePath + fileName.lower()

	img.convert('RGB').save(filePath)

	fileSize = os.path.getsize(filePath)

	responseData['suffix']    = suffix
	responseData['status']    = 'success'
	responseData['savename']  = fileName,
	responseData['fileName']  = reqfile.name,
	responseData['url']       = url + savepath + fileName
	responseData['fileSize']  = C.getSizeInNiceString(fileSize)
	responseData['savepath']  = time.strftime('%Y/%m/',time.localtime(time.time())),
	
	userId = request.session.get('user_id', 0)

	Attachment.objects.create(
		suffix         = suffix,
		type           = 'image',
		savename       = fileName,
		update_time    = time.time(),
		create_time    = time.time(),
		user_id        = int(userId),
		original_name  = reqfile.name,
		size           = int(fileSize),
		savepath       = time.strftime('%Y/%m/',time.localtime(time.time()))
	)

	return HttpResponse(json.dumps(responseData), content_type='application/json')
Esempio n. 2
0
def attachment(request):
    if C.checkLoginAdmin(request.session.get('uInfo', False)) == False:
        return HttpResponseRedirect('/signin/')
    else:
		context = {}
		sql = 'SELECT username, attrch_id, original_name, create_time, size '
		sql += 'FROM "' + Meta.db_table + '_attachment" AS attach LEFT JOIN "' + Meta.db_table +'_user" AS userx '
		sql += 'ON userx.user_id=attach.user_id '
		
		word = ''
		if request.GET.get('word'):
			word = str(cgi.escape(request.GET.get('word')))
			sql += "WHERE attach.original_name LIKE '%%" + str(cgi.escape(request.GET.get('word'))) + "%%' "

		sql += 'ORDER BY attrch_id DESC'
		
		attachmentList = Attachment.objects.raw(sql)
		attachmentList = list(attachmentList)

		paginator  = Paginator(attachmentList, 10)
		page       = int(request.GET.get('page', 1))

		try:
			pagebar = paginator.page(page)
		except PageNotAnInteger:
			pagebar = paginator.page(1)
		except EmptyPage:
			pagebar = paginator.page(paginator.num_pages)

		for item in pagebar:
			item.size = C.getSizeInNiceString(int(item.size))
			item.create_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(item.create_time))

		context = {
			'word'     : word,
			'pagebar'  : pagebar
		}

		return render(request, manageThemeDir + 'attachment.html', context)