-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
madlee.sjtu
committed
Oct 19, 2008
1 parent
faf7025
commit b2c399a
Showing
8 changed files
with
153 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,108 @@ | ||
#!/usr/bin/env python | ||
#coding=utf-8 | ||
|
||
|
||
from uploads.models import * | ||
from uploads.config import UPLOAD_FILE_FOLDER | ||
from uploads.config import MAX_THUMBNAIL_SIZE | ||
|
||
from settings import MEDIA_ROOT | ||
from md5 import md5 | ||
from cStringIO import StringIO | ||
|
||
import os.path | ||
import time | ||
from datetime import datetime | ||
import PIL | ||
import uploads.exif | ||
import Image, ImageDraw # 需要PIL支持 | ||
|
||
def get_ext_name(fullname): | ||
"""获取文件的扩展名。(不包括.)""" | ||
"""获取文件的扩展名。(不包括.)""" | ||
i = fullname.find('.') | ||
result = "" | ||
if i > -1: | ||
result = fullname[i+1:] | ||
if len(result) > 10: | ||
result = "" | ||
else: | ||
result = result.lower() | ||
return result | ||
|
||
def getTakeTime(content): | ||
"""获取照片的拍摄时间""" | ||
def get_take_time(content): | ||
"""获取照片的拍摄时间""" | ||
file = StringIO(content) | ||
tag = exif.process_file(file) | ||
tag = uploads.exif.process_file(file) | ||
file.close() | ||
originalTime = str(tag["EXIF DateTimeOriginal"]) | ||
if originalTime == None: | ||
return None | ||
else: | ||
result = "'" + originalTime[0:4] + "/" + originalTime[5:7] + "/" + originalTime[8:] + "'" | ||
print result | ||
result = time.strptime("%Y/%m/%d %H%M%S", result) | ||
return result | ||
|
||
def saveFile(user, i): | ||
"""保存上传文件并返回一个UploadResource记录。修改此函数以修改文件的存储过程""" | ||
|
||
if originalTime != None: | ||
try: | ||
result = time.strptime(originalTime, "%Y:%m:%d %H:%M:%S") | ||
return datetime(result[0], result[1], result[2], result[3], result[4], result[5]) | ||
except ValueError, error: | ||
pass | ||
return None | ||
|
||
def save_uploaded_file(user, i): | ||
"""保存上传文件并返回一个UploadResource记录。修改此函数以修改文件的存储过程""" | ||
extname = get_ext_name(i.name) | ||
try: | ||
e.objects.get(extname__exact=extname) | ||
mime = MimeType.objects.get(extname__exact=extname) | ||
except MimeType.DoesNotExist: | ||
mime = MimeType() | ||
mime.extname = extname | ||
mime.minor = extname | ||
mime.save() | ||
|
||
content = i.read() | ||
if mime.magic_number != "" and not content.startswith(mime.magic_number): | ||
raise IllegalFileFormat(i.name, mime) | ||
|
||
filename = strftime(UPLOAD_FILE_FOLDER) | ||
filename = time.strftime(UPLOAD_FILE_FOLDER) | ||
filename = os.path.join(filename, i.name) | ||
filename = os.path.join(MEDIA_ROOT, folder) | ||
realname = filename[:] | ||
|
||
dest = os.path.join(MEDIA_ROOT, filename) | ||
|
||
count = 0 | ||
while os.path.isdir(realname): | ||
realname = filename[0:-len(extname)] + str(count) + "." + extname | ||
|
||
content = i.read() | ||
file = open(filename, "wb") | ||
while os.path.isfile(dest): | ||
count += 1 | ||
dest = os.path.join(MEDIA_ROOT, filename[0:-len(extname)] + str(count) + "." + extname) | ||
|
||
if count > 0: | ||
filename = filename[0:-len(extname)] + str(count) + "." + extname | ||
|
||
file = open(dest, "wb") | ||
file.write(content) | ||
file.close() | ||
|
||
result = UploadResource() | ||
result.filename = filename | ||
result.mime = mime | ||
result.owner = user | ||
result.md5code = md5(content).hexdigest() | ||
|
||
if mime.major == 'image': | ||
result.taketime = getTakeTime(content) | ||
if mime.major == u'image': | ||
result.taketime = get_take_time(content) | ||
|
||
result.save() | ||
|
||
return result | ||
|
||
|
||
def getThumbImage(id, x, y): | ||
def get_thumb_image(id, x, y): | ||
"""获取缩略图""" | ||
x = int(x) | ||
if x > MAX_THUMBNAIL_SIZE: | ||
x = MAX_THUMBNAIL_SIZE | ||
|
||
y = int(y) | ||
if y > MAX_THUMBNAIL_SIZE: | ||
y = MAX_THUMBNAIL_SIZE | ||
|
||
try: | ||
record = UploadResource.get(id=id) | ||
record.getFile() | ||
except UploadResource.DoesNotExist: | ||
record = UploadResource.objects.get(id=id) | ||
result = Image.open(record.filename.path) | ||
result.thumbnail((x, y), Image.ANTIALIAS) | ||
return result | ||
except Exception, error: | ||
return None | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<title>index</title> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<title>Upload</title> | ||
</head> | ||
|
||
<body> | ||
<form action="" method="post" enctype="multipart/form-data" name="form1" target="." id="form1"> | ||
<label> | ||
<input type="file" name="file" /> | ||
</label> | ||
<label> | ||
<input type="submit" name="Submit" value="Submit" /> | ||
</label> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
<title>上传文件</title> | ||
</head> | ||
|
||
<body> | ||
<p>{{ user.username }} | ||
刚刚上传了 如下文件:</p> | ||
<p>{% for i in files %}</p> | ||
<ul> | ||
<li><a href="{{ i.filename.url }}">{{ i.filename.name }} </a>,是一个{{ i.mime }} 文件,共{{ i.filename.size }}字节</li> | ||
</ul> | ||
<p>{% endfor %}</p> | ||
<p> </p> | ||
</body> | ||
</html> |