Skip to content

Instantly share code, notes, and snippets.

View muratgozel's full-sized avatar

Murat Gözel muratgozel

View GitHub Profile
@muratgozel
muratgozel / nodejs-simple-file-server.js
Created November 28, 2019 12:07
Simple node.js server that serves files under a certain directory.
/*
*
* usage: node server.js folder port
* - folder is "." by default which is the current directory where node has been executed.
* - port is "8080" by default and you can specify any available port in your OS.
*
*/
const http = require('http')
const fs = require('fs')
function configureTimeAgo(element) {
const elementID = element.getAttribute('id')
let updater = setInterval(function() {
const elementStillExistInDOM = document.getElementById(elementID)
if (!elementStillExistInDOM) {
clearInterval(updater)
return;
}
const seconds = (Date.now() - parseFloat(element.dataset.timestamp)) / 1000
@muratgozel
muratgozel / file_changed.sh
Created April 23, 2019 08:13
Compares md5 hashes of the files and returns boolean in bash.
#!/bin/bash
function file_changed {
# compares md5 hashes of the files and returns boolean
# usage: if file_changed file1.ext file2.ext; then ...
local return_=1
# get md5 hash of the first file
local m=($(openssl md5 $1))
#!/bin/bash
# set env vars from .env file
set -a
. $1
set +a
# set backup filename
datetime=$(date +%Y-%m-%d-%H-%M-%S)
filename="${datetime}.gz"
@muratgozel
muratgozel / .browserslistrc
Last active October 31, 2019 11:01
Browserslist query practice.
last 3 versions
> 0.1%
not ie <= 9
# enter "npx browserslist" in the terminal to see which browsers supported.
mongodb://$user:$password@localhost:27027/?authMechanism=DEFAULT&authSource=$dbname
mongodb://$user:$password@$host1:27017,$host2:27017/?replicaSet=rs0&authMechanism=DEFAULT&authSource=$dbname
@muratgozel
muratgozel / doesnt_exist.sh
Last active February 6, 2019 12:46
Some useful bash functions to check if something doesn't exist in Linux OS.
# check if user doesn't exist
function user_doesnt_exist {
if id -u "$1" >/dev/null 2>&1; then
return 1
else
return 0
fi
}
# usage. do something if the user abc doesn't exist.
@muratgozel
muratgozel / gen-static-pages.js
Last active March 12, 2024 03:25
Static page generation with puppeteer headless chrome for javascript web apps.
const os = require('os')
const crypto = require('crypto')
const path = require('path')
const fs = require('fs')
const util = require('util')
const puppeteer = require('puppeteer')
const libxml = require('libxmljs')
const dayjs = require('dayjs')
// generates static html files for server side rendering
@muratgozel
muratgozel / resize-and-crop-with-pillow.py
Created December 1, 2018 13:55
Resizes and crops the given image using Python Pillow. Support multi frame images such as GIF and WebP.
from math import floor, fabs
from PIL import Image, ImageSequence
def transform_image(original_img, crop_w, crop_h):
"""
Resizes and crops the image to the specified crop_w and crop_h if necessary.
Works with multi frame gif and webp images also.
args:
original_img is the image instance created by pillow ( Image.open(filepath) )
@muratgozel
muratgozel / install_pillow.sh
Created November 28, 2018 15:39
install pillow (python imaging library) on ubuntu 18.04 (bionic)
#!/bin/bash
apt update
apt install python3-pip -y
apt install libjpeg8-dev zlib1g-dev libtiff-dev libfreetype6 libfreetype6-dev libwebp-dev libopenjp2-7-dev libopenjp2-7-dev -y
pip3 install pillow --global-option="build_ext" --global-option="--enable-zlib" --global-option="--enable-jpeg" --global-option="--enable-tiff" --global-option="--enable-freetype" --global-option="--enable-webp" --global-option="--enable-webpmux" --global-option="--enable-jpeg2000"