Using a UUID as a primary key in Django models (generic relations impact)

As seen in the documentation, from Django 1.8 there is a built in UUID field. The performance differences when using a UUID vs integer are negligible. import uuid from django.db import models class MyUUIDModel(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) You can also check this answer for more information.

Determine file type of an image

A probably easier approach would be to use Image.FromFile() and then use the RawFormat property, as it already knows about the magic bits in the headers for the most common formats, like this: Image i = Image.FromFile(“c:\\foo”); if (System.Drawing.Imaging.ImageFormat.Jpeg.Equals(i.RawFormat)) MessageBox.Show(“JPEG”); else if (System.Drawing.Imaging.ImageFormat.Gif.Equals(i.RawFormat)) MessageBox.Show(“GIF”); //Same for the rest of the formats

Utility of HTTP header “Content-Type: application/force-download” for mobile?

Content-Type: application/force-download means “I, the web server, am going to lie to you (the browser) about what this file is so that you will not treat it as a PDF/Word Document/MP3/whatever and prompt the user to save the mysterious file to disk instead”. It is a dirty hack that breaks horribly when the client doesn’t … Read more

What are all the possible values for HTTP “Content-Type” header?

You can find every content type here: http://www.iana.org/assignments/media-types/media-types.xhtml The most common type are: Type application application/java-archive application/EDI-X12 application/EDIFACT application/javascript application/octet-stream application/ogg application/pdf application/xhtml+xml application/x-shockwave-flash application/json application/ld+json application/xml application/zip application/x-www-form-urlencoded Type audio audio/mpeg audio/x-ms-wma audio/vnd.rn-realaudio audio/x-wav Type image image/gif image/jpeg image/png image/tiff image/vnd.microsoft.icon image/x-icon image/vnd.djvu image/svg+xml Type multipart multipart/mixed multipart/alternative multipart/related (using by MHTML (HTML mail).) … Read more

How to get the content-type of a file in PHP?

I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results: function getFileMimeType($file) { if (function_exists(‘finfo_file’)) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $type = finfo_file($finfo, $file); finfo_close($finfo); } else { require_once ‘upgradephp/ext/mime.php’; $type = mime_content_type($file); } if (!$type || in_array($type, array(‘application/octet-stream’, ‘text/plain’))) { $secondOpinion = exec(‘file -b –mime-type … Read more