Test server Accept-Range with cURL

June 22nd, 2015 by Alex No comments »

In case you want to make sure your videos on your very own web server can handle seeking, here is a very simple command to test it:

curl -r 1-20000 http://myurl.mp4 -o test.mp4

If the result test.mp4 is 20KB, it works.

Enable Apache2 mod_rewrite on Linux

September 22nd, 2014 by Alex No comments »

There is a special tool for that, called a2enmod, so we can just run

a2enmod rewrite

Or sudo if you are not root

sudo a2enmod rewrite

Then, restart apache

service apache2 restart

Download all images from current page with JavaScript

August 18th, 2014 by Alex No comments »
// get possible elements, init variables
var elems = document.querySelectorAll('*'), elems2 = document.querySelectorAll('img');
var images = {}, a = 0, href = '';
// search for CSS background-image
for (a = 0; a < elems.length; a++) {
  style = elems[a].currentStyle || window.getComputedStyle(elems[a], false);
  var bg = style.backgroundImage.slice(4, -1);
  if (bg) {
    images[bg] = bg;
// search for <img src="" />
for (a = 0; a < elems2.length; a++) {
  if (href = elems2[a].getAttribute('src'))
    images[href] = href;
// generate <a> and force download
for (var i in images) {
  var link = document.createElement('a');
  link.setAttribute('download', '');
  link.setAttribute('href', i);
  delete link;

Adding a message with JavaScript before leaving the page

June 20th, 2014 by Alex 1 comment »

It is very very easy, the callback you add should return a string that will be prepended to the confirmation message. Plain javascript:

window.onbeforeunload = function(){
  return 'You are about to lose your changes!';

Or with jQuery if you wish so

$(window).on('beforeunload', function(){
  return 'Are you sure you want to leave?';

Searching for a value in a combined MySQL column

May 18th, 2014 by Alex 2 comments »

This time we will have to search for a value inside a comma-delimited column. Let’s say we have this structure:

name  | location
Alex  | Philadelphia, United States
Bob   | Athens, Greece
John  | London, England
Bill  | Kiev, Ukraine
Mark  | Warsaw, Poland
Harry | Odessa, Ukraine
Jane  | Chicago, United States

First, let’s make sure the SPLIT_STR function exists in our database:

  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

Now we need to get all people from Ukraine, what we can do is create a new temporary table, split the location in two columns and then we can perform a search.

SELECT `name`, 
  TRIM(SPLIT_STR(`location`, ',', 1)) AS `location1`, 
  TRIM(SPLIT_STR(`location`, ',', 2)) AS `location2` 
FROM `users`;

We might want to additionally add an index while we are doing our magic:

ALTER TABLE `users_new` ADD INDEX (  `location2` )

And now we can simply SELECT from our new temporary table. Note, that all the queries need to be run in one connection, because temporary table will go away as soon as we disconnect (or our script disconnects).

SELECT * FROM `users_new` WHERE `location2` = "Ukraine";