Commit | Line | Data |
---|---|---|
41f446f4 CAW |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011 Free Software Foundation, Inc | |
3 | # | |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | import Image | |
254bc431 | 18 | from mediagoblin.db.util import ObjectId |
41f446f4 CAW |
19 | from celery.task import task |
20 | ||
300c34e8 | 21 | from mediagoblin import mg_globals as mgg |
41f446f4 CAW |
22 | |
23 | ||
24 | THUMB_SIZE = 200, 200 | |
25 | ||
26 | ||
180bdbde | 27 | def create_pub_filepath(entry, filename): |
48a7ba1e | 28 | return mgg.public_store.get_unique_filepath( |
180bdbde E |
29 | ['media_entries', |
30 | unicode(entry['_id']), | |
31 | filename]) | |
32 | ||
33 | ||
41f446f4 CAW |
34 | @task |
35 | def process_media_initial(media_id): | |
300c34e8 | 36 | workbench = mgg.workbench_manager.create_workbench() |
ca030ab6 | 37 | |
300c34e8 | 38 | entry = mgg.database.MediaEntry.one( |
254bc431 | 39 | {'_id': ObjectId(media_id)}) |
41f446f4 | 40 | |
fa7f9c61 | 41 | queued_filepath = entry['queued_media_file'] |
52426ae0 E |
42 | queued_filename = workbench.localized_file( |
43 | mgg.queue_store, queued_filepath, | |
ca030ab6 CAW |
44 | 'source') |
45 | ||
46 | queued_file = file(queued_filename, 'r') | |
41f446f4 CAW |
47 | |
48 | with queued_file: | |
fa7f9c61 | 49 | thumb = Image.open(queued_file) |
41f446f4 | 50 | thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS) |
34d35a23 | 51 | # ensure color mode is compatible with jpg |
52 | if thumb.mode != "RGB": | |
53 | thumb = thumb.convert("RGB") | |
41f446f4 | 54 | |
180bdbde | 55 | thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg') |
41f446f4 | 56 | |
300c34e8 | 57 | thumb_file = mgg.public_store.get_file(thumb_filepath, 'w') |
894facc6 | 58 | with thumb_file: |
41f446f4 CAW |
59 | thumb.save(thumb_file, "JPEG") |
60 | ||
fa7f9c61 CAW |
61 | # we have to re-read because unlike PIL, not everything reads |
62 | # things in string representation :) | |
ca030ab6 | 63 | queued_file = file(queued_filename, 'rb') |
fa7f9c61 CAW |
64 | |
65 | with queued_file: | |
180bdbde | 66 | main_filepath = create_pub_filepath(entry, queued_filepath[-1]) |
fa7f9c61 | 67 | |
300c34e8 | 68 | with mgg.public_store.get_file(main_filepath, 'wb') as main_file: |
fa7f9c61 CAW |
69 | main_file.write(queued_file.read()) |
70 | ||
300c34e8 | 71 | mgg.queue_store.delete_file(queued_filepath) |
180bdbde | 72 | entry['queued_media_file'] = [] |
fa7f9c61 CAW |
73 | media_files_dict = entry.setdefault('media_files', {}) |
74 | media_files_dict['thumb'] = thumb_filepath | |
75 | media_files_dict['main'] = main_filepath | |
12b6ecac CAW |
76 | entry['state'] = u'processed' |
77 | entry.save() | |
ca030ab6 CAW |
78 | |
79 | # clean up workbench | |
b67a983a | 80 | workbench.destroy_self() |