Upgrade to Pro — share decks privately, control downloads, hide ads and more …

15-437 Files

ThierrySans
September 17, 2013

15-437 Files

ThierrySans

September 17, 2013
Tweet

More Decks by ThierrySans

Other Decks in Education

Transcript

  1. MIME types MIME (Multipurpose Internet Mail Extensions)
 is also known

    as the content type ➡ Define the format of a document exchanged on internet 
 (IETF standard) http://www.iana.org/assignments/media-types/index.html
  2. Examples of MIME types • text/html • text/css • text/javascript

    • image/jpeg - image/gif - image/svg - image/png (and so on) • application/pdf • application/json
  3. Example of how images are retrieved <html>! <body>! <img src=images/bart.jpg/>!

    </body>! </html> GET hello/bart/ http://localhost/HelloYou/ http://www.example.com//hello/bart/ GET images/bart.jpg MIME : text/html MIME : image/jpg
  4. Upload FORM <form enctype="multipart/form-data" action="{% url 'add' %}" method="post">! <input

    id="upload" type="file" name="file"/>! <input type="text" name="name"/>! <input type="text" name="website"/>! </form>! <a id="publishButton">Publish</div> WebDirectory/templates/WebDirectory/index.html this form handles multiple formats of data the URL to submit the form uploading files must be done through a POST request
  5. Saving uploaded files Controller def add(request):! ... Database img mime

    path image/png path image/jpg The image path and the mime type are stored in the database POST add/ uploads The image is stored in the upload directory Image
  6. Configuring Django about where to upload files # Absolute filesystem

    path to the directory that will hold user-uploaded files.! # Example: "/home/media/media.lawrence.com/media/"! MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads/') tsans/settings.py
  7. The model class Entry(models.Model):! # image = models.CharField(max_length=200)! name =

    models.CharField(max_length=200)! webpage = models.URLField(max_length=200)
 ! class Image(models.Model):! ! entry = models.ForeignKey(Entry, unique=True)! image = models.ImageField(upload_to='WebDirectory')! mimeType = models.CharField(max_length=20) WebDirectory/models.py where to store uploaded files use FileField for generic files We need to record the MIME type (see serving uploaded file) entry foreign key
  8. Controller - saving uploaded files from django.views.decorators.csrf import csrf_exempt! !

    @csrf_exempt! def add(request):! e = Entry(name = request.POST['name'],\! webpage = request.POST['website'])! e.save()
 ! i = Image(entry = e,\! image = request.FILES['file'],\! mimeType=request.FILES['file'].content_type)! i.save()
 ! return HttpResponseRedirect(reverse('index'))! WebDirectory/views.py get the file from the HTTP request Not secure but we will talk about security later get the MIME type
  9. Controller - serving uploaded files • How to serve these

    uploaded files? • Should we serve them as static files?
  10. Why not serving uploaded files as static files? Because we

    want to control • who can access these files • when to serve these files • how to serve these files
  11. A good way to serve uploaded files Have a method

    to control (controller) the file - getImage Reference the image using an identifier • automatically generated hash code • or database entry id (primary key in the database)
  12. How to define getImage Controller def getimage(request,imageid):! ... GET getimage/345/

    uploads Retrieve the image from the upload directory
 (this is done automatically by Django) Database img mime path image/png path image/jpg Get the image path and mime type from the database based on its id Return a HTTP response of the corresponding mime type Image
  13. Controller - serving uploaded files urlpatterns = patterns('',! url(r'^$', views.index,

    name='index'),! url(r'^add/$', views.add, name='add'),! url(r'^getimage/(?P<image_id>\d+)/$',\! views.getImage,\! name='getImage'),! ) WebDirectory/urls.py
  14. Controller - serving uploaded files def getImage(request,image_id):! i = Image.objects.get(id=image_id)!

    return HttpResponse(i.image.read(), content_type=i.mimeType)! ! def index(request):! ! image_list = Image.objects.all()! return render(request,’WebDirectory/index.html’,\
 {'image_list': image_list}) WebDirectory/views.py get the file from the database return an HTTP response of the corresponding MIME type
  15. Updating the image URL in the template WebDirectory/templates/WebDirectory/index.html ...! <div

    id="directory">! {% if image_list %}! {% for image in image_list %}! <div class="entry">! <div class="image"><img src="{% url 'getImage' image.id %}"/>! </div>! <div class=“name">{{image.entry.name}}</div>! <div class="website">! <a href=“{{image.entry.webpage}}”>
 ! ! ! {{image.entry.name}}'s website
 ! ! </a>! ...