0

I'm working in an API with Django Rest Framework and I need to store the full URL for an image in my db. I think in that way is easiest to send it via json.

I receive the image from POST request. I am using a RESTfull tool client to test, I get something like this in the view post method:

<InMemoryUploadedFile: profile_1.jpg (image/jpeg)>

In my model I have this:

photo_url = models.URLField(null=True)

My original idea was store it in a external server and store the url, but now I need to store the image in my own server but store only the url in my db. How can I achieve this? Thanks!

M.javid
  • 6,387
  • 3
  • 41
  • 56
Gocht
  • 9,924
  • 3
  • 42
  • 81
  • Did you ever read [managing files](https://docs.djangoproject.com/en/1.8/topics/files/) section in Django documentation? – Ozgur Vatansever Aug 10 '15 at 21:50
  • @ozgur Yes I did, but it's about text files, when talk about images it's with a ImageField in a model or in a Form. This is an API I just receive a JSON in my backend. – Gocht Aug 10 '15 at 21:54
  • DRF supports returning the url to the file with an `ImageField`, and there are plenty of [custom `ImageField` subclasses](http://stackoverflow.com/q/28036404/359284) that you can use for JSON. Is there any reason why you _have_ to use a `URLField` in your model? – Kevin Brown-Silva Aug 15 '15 at 02:50
  • Model was made when I take this project, and I don't want to change it. – Gocht Aug 15 '15 at 13:45

1 Answers1

0

I achieved this by doing as follow:

def get_encoded_image(encoded_image=None):

    """
    Save the received in request base64 encoded image and return the image path to serve.
    """

    _file = str(encoded_image)
    _file = base64.b64decode(_file)
    path = default_storage.save('{folder}/{file_name}.{extension}'.format(folder=settings.DEFAULT_PROFILE_IMAGES_FOLDER,
                                                                          file_name=time.time().__str__(),
                                                                          extension='jpg'),

                                ContentFile(_file))
    if settings.DEBUG:
        image_url = urljoin(settings.DEV_DOMAIN, '{media}{path}'.format(media=settings.MEDIA_URL,
                                                                        path=path))
    else:
        image_url = urljoin(settings.DOMAIN, '{media}{path}'.format(media=settings.MEDIA_URL,
                                                                    path=path))
    return image_url

I received the image encoded with base64 in a POST request. I used the .time module to get a unique name, but Django adds a hash at the end of the file name if there's another with the same name.

Note variables like:

  • DEFAULT_PROFILE_IMAGES_FOLDER: Name of the folder to store the images (inside media folder).
  • DEBUG: Django's debug variable.
  • DEV_DOMAIN: In my case, this is my http://localhost:8000
  • DOMAIN: Server's IP or domain where application is running.
Gocht
  • 9,924
  • 3
  • 42
  • 81