def serve_media(request, path): full_path = os.path.join(settings.MEDIA_ROOT, path) if not os.path.exists(full_path): storage = DropboxStorage(location=settings.DROPBOX_LOCATION) if not storage.exists(path): raise Http404 dir_name = os.path.dirname(full_path) if not os.path.exists(dir_name): os.makedirs(dir_name) with open(full_path, "wb") as f: image = storage.open(path) f.write(image.read()) image.close() statobj = os.stat(full_path) mimetype, encoding = mimetypes.guess_type(full_path) mimetype = mimetype or "application/octet-stream" with open(full_path, "rb") as f: response = HttpResponse(f.read(), mimetype=mimetype) if not settings.DEBUG: os.remove(full_path) response["Last-Modified"] = http_date(statobj.st_mtime) if stat.S_ISREG(statobj.st_mode): response["Content-Length"] = statobj.st_size if encoding: response["Content-Encoding"] = encoding return response
class DropboxStorageTest(TestCase): def setUp(self): self.location = '/Public/testing' self.storage = DropboxStorage(location=self.location) self.tmpfile = tempfile.NamedTemporaryFile() def tearDown(self): self.tmpfile.close() def test_file_access(self): """ Standard file access options are available, and work as expected. """ self.assertFalse(self.storage.exists('storage_test')) with self.storage.open('storage_test', 'w') as f: f.write('storage contents') self.assertTrue(self.storage.exists('storage_test')) with self.storage.open('storage_test', 'r') as f: self.assertEqual(f.read(), 'storage contents') self.storage.delete('storage_test') self.assertFalse(self.storage.exists('storage_test')) def test_exists_folder(self): self.assertFalse(self.storage.exists('storage_test_exists')) self.storage.client.file_create_folder(self.location + '/storage_test_exists') self.assertTrue(self.storage.exists('storage_test_exists')) self.storage.delete('storage_test_exists') self.assertFalse(self.storage.exists('storage_test_exists')) def test_listdir(self): """ File storage returns a tuple containing directories and files. """ self.assertFalse(self.storage.exists('storage_test_1')) self.assertFalse(self.storage.exists('storage_test_2')) self.assertFalse(self.storage.exists('storage_dir_1')) f = self.storage.save('storage_test_1', ContentFile('custom content')) f = self.storage.save('storage_test_2', ContentFile('custom content')) self.storage.client.file_create_folder(self.location + '/storage_dir_1') dirs, files = self.storage.listdir(self.location) self.assertEqual(set(dirs), set([u'storage_dir_1'])) self.assertEqual(set(files), set([u'storage_test_1', u'storage_test_2'])) self.storage.delete('storage_test_1') self.storage.delete('storage_test_2') self.storage.delete('storage_dir_1') def test_file_url(self): """ File storage returns a url to access a given file from the Web. """ # url = self.share(path) # self.assertRaises(ValueError, self.storage.url, 'test.file') pass def test_file_size(self): """ File storage returns a url to access a given file from the Web. """ self.assertFalse(self.storage.exists('storage_test_size')) contents = "here are some file contents." with self.storage.open('storage_test_size', 'w') as f: f.write(contents) self.assertTrue(self.storage.exists('storage_test_size')) with self.storage.open('storage_test_size', 'r') as f: self.assertEqual(f.size, len(contents)) self.storage.delete('storage_test_size') self.assertFalse(self.storage.exists('storage_test_size'))
class DropboxStorageTest(TestCase): def setUp(self): self.location = '/Public/testing' self.storage = DropboxStorage(location=self.location) self.storage.base_url = '/test_media_url/' def test_file_access_options(self): """ Standard file access options are available, and work as expected. """ self.assertFalse(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'w') f.write('storage contents') f.close() self.assertTrue(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'r') self.assertEqual(f.read(), 'storage contents') f.close() self.storage.delete('storage_test') self.assertFalse(self.storage.exists('storage_test')) def test_exists_folder(self): self.assertFalse(self.storage.exists('storage_test_exists')) self.storage.client.file_create_folder(self.location + '/storage_test_exists') self.assertTrue(self.storage.exists('storage_test_exists')) self.storage.delete('storage_test_exists') self.assertFalse(self.storage.exists('storage_test_exists')) def test_listdir(self): """ File storage returns a tuple containing directories and files. """ self.assertFalse(self.storage.exists('storage_test_1')) self.assertFalse(self.storage.exists('storage_test_2')) self.assertFalse(self.storage.exists('storage_dir_1')) f = self.storage.save('storage_test_1', ContentFile('custom content')) f = self.storage.save('storage_test_2', ContentFile('custom content')) self.storage.client.file_create_folder(self.location + '/storage_dir_1') dirs, files = self.storage.listdir(self.location) self.assertEqual(set(dirs), set([u'storage_dir_1'])) self.assertEqual(set(files), set([u'storage_test_1', u'storage_test_2'])) self.storage.delete('storage_test_1') self.storage.delete('storage_test_2') self.storage.delete('storage_dir_1') def test_file_size(self): """ File storage returns a url to access a given file from the Web. """ self.assertFalse(self.storage.exists('storage_test_size')) f = self.storage.open('storage_test_size', 'w') f.write('these are 18 bytes') f.close() self.assertTrue(self.storage.exists('storage_test_size')) f = self.storage.open('storage_test_size', 'r') self.assertEqual(f.size, 18) f.close() self.storage.delete('storage_test_size') self.assertFalse(self.storage.exists('storage_test_size'))
class DropboxStorageTest(TestCase): def setUp(self): """Setup any state specific to the execution of the given module.""" self.location = '/Public/testing' self.storage = DropboxStorage(location=self.location) self.storage.base_url = '/test_media_url/' self.storage.delete('/Public/testing') def tearDown(self): """Teardown any state that was previously setup with a setup.""" self.storage.delete('/Public/testing') def test_file_create(self): """File storage should delete file.""" self.assertFalse(self.storage.exists('storage_test_1')) self.storage.save('storage_test_1', ContentFile('custom content')) self.assertTrue(self.storage.exists('storage_test_1')) self.assertFalse(self.storage.exists('storage_test_2')) self.storage.save('storage_test_2', ContentFile('custom content')) self.assertTrue(self.storage.exists('storage_test_2')) def test_file_delete(self): """File storage should delete file.""" self.assertFalse(self.storage.exists('storage_test_1')) self.storage.save('storage_test_1', ContentFile('custom content')) self.assertTrue(self.storage.exists('storage_test_1')) self.storage.delete('storage_test_1') self.assertFalse(self.storage.exists('storage_test_1')) def test_file_delete2(self): """File storage should delete dir.""" self.storage.delete('storage_dir_1') self.assertFalse(self.storage.exists('storage_dir_1')) self.storage.client.file_create_folder(self.location + '/storage_dir_1') self.assertTrue(self.storage.exists('storage_dir_1')) self.storage.delete('storage_dir_1') self.assertFalse(self.storage.exists('storage_dir_1')) def test_file_access_options(self): """Standard file access options are available, and work as expected.""" self.assertFalse(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'w') f.write('storage contents') f.close() self.assertTrue(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'r') self.assertEqual(f.read(), six.b('storage contents')) f.close() self.storage.delete('storage_test') self.assertFalse(self.storage.exists('storage_test')) def test_exists_folder(self): self.storage.delete('storage_test_exists') self.assertFalse(self.storage.exists('storage_test_exists')) self.storage.client.file_create_folder(self.location + '/storage_test_exists') self.assertTrue(self.storage.exists('storage_test_exists')) self.storage.delete('storage_test_exists') self.assertFalse(self.storage.exists('storage_test_exists')) def test_listdir(self): """File storage returns a tuple containing directories and files.""" self.assertFalse(self.storage.exists('storage_test_1')) self.assertFalse(self.storage.exists('storage_test_2')) self.assertFalse(self.storage.exists('storage_dir_1')) self.storage.save('storage_test_1', ContentFile(six.b('custom content'))) self.storage.save('storage_test_2', ContentFile(six.b('custom content'))) self.storage.client.file_create_folder(self.location + '/storage_dir_1') dirs, files = self.storage.listdir(self.location) self.assertEqual(set(dirs), set([u'storage_dir_1'])) self.assertEqual(set(files), set([u'storage_test_1', u'storage_test_2'])) def test_file_size(self): """File storage returns a url to access a given file from the Web.""" self.assertFalse(self.storage.exists('storage_test_size')) f = self.storage.open('storage_test_size', 'w') f.write('these are 18 bytes') f.close() self.assertTrue(self.storage.exists('storage_test_size')) f = self.storage.open('storage_test_size', 'r') self.assertEqual(f.size, 18) f.close() self.storage.delete('storage_test_size') self.assertFalse(self.storage.exists('storage_test_size'))
class DropboxStorageTest(TestCase): def setUp(self): self.location = '/Public/testing' self.storage = DropboxStorage(location=self.location) self.storage.base_url = '/test_media_url/' def test_file_access_options(self): """ Standard file access options are available, and work as expected. """ self.assertFalse(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'w') f.write('storage contents') f.close() self.assertTrue(self.storage.exists('storage_test')) f = self.storage.open('storage_test', 'r') self.assertEqual(f.read(), 'storage contents') f.close() self.storage.delete('storage_test') self.assertFalse(self.storage.exists('storage_test')) def test_exists_folder(self): self.assertFalse(self.storage.exists('storage_test_exists')) self.storage.client.file_create_folder(self.location + '/storage_test_exists') self.assertTrue(self.storage.exists('storage_test_exists')) self.storage.delete('storage_test_exists') self.assertFalse(self.storage.exists('storage_test_exists')) def test_listdir(self): """ File storage returns a tuple containing directories and files. """ self.assertFalse(self.storage.exists('storage_test_1')) self.assertFalse(self.storage.exists('storage_test_2')) self.assertFalse(self.storage.exists('storage_dir_1')) f = self.storage.save('storage_test_1', ContentFile('custom content')) f = self.storage.save('storage_test_2', ContentFile('custom content')) self.storage.client.file_create_folder(self.location + '/storage_dir_1') dirs, files = self.storage.listdir(self.location) self.assertEqual(set(dirs), set([u'storage_dir_1'])) self.assertEqual(set(files), set([u'storage_test_1', u'storage_test_2'])) self.storage.delete('storage_test_1') self.storage.delete('storage_test_2') self.storage.delete('storage_dir_1') def test_file_url(self): """ File storage returns a url to access a given file from the Web. """ self.assertEqual(self.storage.url('test.file'), '%s%s' % (self.storage.base_url, 'test.file')) # should encode special chars except ~!*()' # like encodeURIComponent() JavaScript function do self.assertEqual(self.storage.url(r"""~!*()'@#$%^&*abc`+=.file"""), """/test_media_url/~!*()'%40%23%24%25%5E%26*abc%60%2B%3D.file""") # should stanslate os path separator(s) to the url path separator self.assertEqual(self.storage.url("""a/b\\c.file"""), """/test_media_url/a/b/c.file""") self.storage.base_url = None self.assertRaises(ValueError, self.storage.url, 'test.file') def test_file_size(self): """ File storage returns a url to access a given file from the Web. """ self.assertFalse(self.storage.exists('storage_test_size')) f = self.storage.open('storage_test_size', 'w') f.write('these are 18 bytes') f.close() self.assertTrue(self.storage.exists('storage_test_size')) f = self.storage.open('storage_test_size', 'r') self.assertEqual(f.size, 18) f.close() self.storage.delete('storage_test_size') self.assertFalse(self.storage.exists('storage_test_size'))