Skip to content

Instantly share code, notes, and snippets.

View fxbeckers's full-sized avatar

François-Xavier Beckers fxbeckers

View GitHub Profile
@fxbeckers
fxbeckers / GZipMiddleware.cs
Last active October 17, 2016 04:35
Pretty naive version of an OWIN/Katana middleware gzipping response stream whose length meets fixed requirements.
public sealed class GZipMiddleware {
private readonly Func<IDictionary<string, object>, Task> next;
public GZipMiddleware(Func<IDictionary<string, object>, Task> next) {
this.next = next;
}
public async Task Invoke(IDictionary<string, object> environment) {
var context = new OwinContext(environment);
//Example usage
//
//var customControllerActivator = new CustomControllerActivator();
//customControllerActivator.AddType(() => new MyApiController());
//httpConfiguration.Services.Replace(typeof(IHttpControllerActivator),customControllerActivator);
class CustomControllerActivator : IHttpControllerActivator
{
readonly Dictionary<Type, Func<IHttpController>> _dictionary = new Dictionary<Type, Func<IHttpController>>();
sudo python -m SimpleHTTPServer 801
@fxbeckers
fxbeckers / NoCacheAttribute.cs
Created July 17, 2013 15:38
No Cache Attribute
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
@fxbeckers
fxbeckers / AllowCrossSiteJsonAttribute.cs
Created July 17, 2013 15:38
Allow Cross Site Json Attribute
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
if (actionExecutedContext.Response != null)
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
base.OnActionExecuted(actionExecutedContext);
}
}
@fxbeckers
fxbeckers / AutoPopover.js
Created May 2, 2013 14:39
Automatically init popovers on click
// jQuery popovers normally have to be initialized on a href with javascript. By hooking in to the click event instead, we can do the same without
// explicitely having to init, which is useful for pages with a dynamic dom
$(document).on('focusin click', '[data-toggle="popover"]', function (e) {
e.preventDefault();
var $element = $(e.target);
if (!$element.has('[data-toggle="popover"]')) {
$element = $element.closest('[data-toggle="popover"]');
}
($element).popover();
});
@fxbeckers
fxbeckers / bucketize
Created April 29, 2013 12:45
Split array into buckets of size n
var bucketize = (x: any[], nInBucket: number) => {
var i = 0.0;
var iX = _.map(x, (x) => { return { i: i++, x: x }; });
var groupedItems = _.groupBy(iX, (ixGrp) => { return Math.floor(ixGrp.i / nInBucket); });
return _.map(_.values(groupedItems), (group: any[]) => {
return _.pluck(group, 'x');
});
}
@fxbeckers
fxbeckers / Finder File Hiding.md
Last active December 16, 2015 07:39
Hiding Files in OS X Finder

File Specific

Show

$ SetFile -a "v" www

Hide

$ SetFile -a "V" www

Finder Defaults

SHOW 'dot' files in Finder.app

$ defaults write com.apple.Finder AppleShowAllFiles YES

@fxbeckers
fxbeckers / Prevent Boot Volume Mount OS X
Last active December 16, 2015 07:39
Prevent a given OS X partition from mounting at boot
An /etc/fstab example:
# Identifier, mount point, fs type, options1
UUID=DF000C7E-AE0C-3B15-B730-DFD2EF15CB91 /export ufs ro
UUID=FAB060E9-79F7-33FF-BE85-E1D3ABD3EDEA none hfs rw,noauto
LABEL=This40Is40The40Volume40Name none msdos ro
The Identifier is used to identify the volume; the LABEL is the volume name, the UUID is the Universal Unique Identifier Drive. You can use both, but the UUID is the best choice because renaming the volume will not change this identifier.
The mount point is the directory used when the volume is mounted; set none to use the pre-defined OS X directory, i.e. ./Volumes/
The fs type describes the type of the filesystem; use hfs for a Mac volume. The field options describes the mount options associated with the filesystem. The option autowill mount the volume in the normal way, noauto will force the volume not to be mounted automatically; and last, use rw or ro for a read-write or read-only disk.
@fxbeckers
fxbeckers / URLEncode NSString Additions
Created March 28, 2011 17:51
Straightforward way of performing URL encoding and decoding of strings, especially useful for use in conjunction with TTNavigtor and URL Mapping
@interface NSString (FXNSStringAdditions)
- (NSString *)urlEncoded;
- (NSString *)urlDecoded;
@end
@implementation NSString (FXNSStringAdditions)
- (NSString *)urlEncoded {