comparison apps/bundle/tasks.py @ 209:4033ebe1867f

Add ability to download files This makes use of a new model (BundleVersion) to keep track of the locations on disk of the original uploads for each version. This will require some manual processing to get it working for existing bundles, since the information needed isn't being stored at the moment.
author dellsystem <ilostwaldo@gmail.com>
date Sun, 17 Feb 2013 14:57:39 -0500
parents b711f0087709
children 95edf106bdef
comparison
equal deleted inserted replaced
188:1eb652aa501a 209:4033ebe1867f
5 5
6 import archive 6 import archive
7 import magic 7 import magic
8 from celery import task 8 from celery import task
9 9
10 from apps.bundle.models import Bundle, BundleFile 10 from apps.bundle.models import Bundle, BundleFile, BundleVersion
11 11
12 12
13 mimetypes.add_type('application/x-gzip', '.tgz') 13 mimetypes.add_type('application/x-gzip', '.tgz')
14 mimetypes.add_type('application/x-bzip2', '.tz2') 14 mimetypes.add_type('application/x-bzip2', '.tz2')
15 archive_extensions = ('.zip', '.tgz', '.tar', '.tz2') 15 archive_extensions = ('.zip', '.tgz', '.tar', '.tz2')
70 70
71 # Figure out the most likely mime-type 71 # Figure out the most likely mime-type
72 mime_type = magic.from_file(file, mime=True) 72 mime_type = magic.from_file(file, mime=True)
73 extension = mimetypes.guess_extension(mime_type) 73 extension = mimetypes.guess_extension(mime_type)
74 74
75 print "mime type: %s" % mime_type
76 print "extension: %s" % extension
77
78 if extension in archive_extensions: 75 if extension in archive_extensions:
79 new_path = file + extension 76 new_path = file + extension
80 # Treat it as an archive. Rename it to that, then extract 77 # Treat it as an archive. Rename it to that, then extract
81 os.rename(file, new_path) 78 os.rename(file, new_path)
79
80 # Create the new BundleVersion
81 new_file_name = os.path.basename(new_path)
82 BundleVersion.objects.create(bundle=bundle, file_name=new_file_name,
83 version=bundle.latest_version)
82 84
83 try: 85 try:
84 # Extract it to a directory, same path as the filename 86 # Extract it to a directory, same path as the filename
85 archive.extract(new_path, to_path=file) 87 archive.extract(new_path, to_path=file)
86 88
87 # Now go through the extracted files, make BundleFiles from them 89 # Now go through the extracted files, make BundleFiles from them
88 process_files_in_dir(bundle, file, None) 90 process_files_in_dir(bundle, file, None)
89 except archive.ArchiveException: 91 except archive.ArchiveException:
90 print "Archive exception"
91 pass 92 pass
92 elif mime_type.startswith('text/'): 93 elif mime_type.startswith('text/'):
93 # Should be a plain text file - create a CodeFile for it 94 # Should be a plain text file - create a CodeFile for it
94 bundle_file = BundleFile(bundle=bundle, name=bundle.file_name, 95 bundle_file = BundleFile(bundle=bundle, name=bundle.file_name,
95 full_path=bundle.file_name, file_size=os.path.getsize(file), 96 full_path=bundle.file_name, file_size=os.path.getsize(file),
96 version=bundle.latest_version) 97 version=bundle.latest_version)
97 bundle_file.save_file_contents(open(file, 'rt'), 98 bundle_file.save_file_contents(open(file, 'rt'),
98 original_filename=bundle.file_name) 99 original_filename=bundle.file_name)
99 100
100 print "Done uploading!"
101 bundle.done_uploading = True 101 bundle.done_uploading = True
102 bundle.save() 102 bundle.save()