I/O error uploading image
Fixed two errors when upload an image from a file. One is the file name doesn't exist error. Another is file got closed in a new thread since Django 1.7. Change-Id: Ia64c385d89e0d938b14182f2744251f76b892eac Closes-Bug: #1451429
This commit is contained in:
parent
d645696f14
commit
810ad30607
@ -26,6 +26,10 @@ import os
|
|||||||
|
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core.files.uploadedfile import InMemoryUploadedFile
|
||||||
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
|
from django.core.files.uploadedfile import TemporaryUploadedFile
|
||||||
|
|
||||||
|
|
||||||
import glanceclient as glance_client
|
import glanceclient as glance_client
|
||||||
from six.moves import _thread as thread
|
from six.moves import _thread as thread
|
||||||
@ -115,9 +119,12 @@ def image_update(request, image_id, **kwargs):
|
|||||||
try:
|
try:
|
||||||
os.remove(image_data.file.name)
|
os.remove(image_data.file.name)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
filename = str(image_data.file)
|
||||||
|
if hasattr(image_data.file, 'name'):
|
||||||
|
filename = image_data.file.name
|
||||||
msg = (('Failed to remove temporary image file '
|
msg = (('Failed to remove temporary image file '
|
||||||
'%(file)s (%(e)s)') %
|
'%(file)s (%(e)s)') %
|
||||||
dict(file=image_data.file.name, e=str(e)))
|
dict(file=filename, e=str(e)))
|
||||||
LOG.warn(msg)
|
LOG.warn(msg)
|
||||||
return image
|
return image
|
||||||
|
|
||||||
@ -130,6 +137,15 @@ def image_create(request, **kwargs):
|
|||||||
image = glanceclient(request).images.create(**kwargs)
|
image = glanceclient(request).images.create(**kwargs)
|
||||||
|
|
||||||
if data:
|
if data:
|
||||||
|
if isinstance(data, TemporaryUploadedFile):
|
||||||
|
# Hack to fool Django, so we can keep file open in the new thread.
|
||||||
|
data.file.close_called = True
|
||||||
|
if isinstance(data, InMemoryUploadedFile):
|
||||||
|
# Clone a new file for InMemeoryUploadedFile.
|
||||||
|
# Because the old one will be closed by Django.
|
||||||
|
data = SimpleUploadedFile(data.name,
|
||||||
|
data.read(),
|
||||||
|
data.content_type)
|
||||||
thread.start_new_thread(image_update,
|
thread.start_new_thread(image_update,
|
||||||
(request, image.id),
|
(request, image.id),
|
||||||
{'data': data,
|
{'data': data,
|
||||||
|
Loading…
Reference in New Issue
Block a user