public static void DownloadBinary(Hashtable htDbBinaryConfig) { HttpRequest request = HttpContext.Current.Request; HttpResponse response = HttpContext.Current.Response; if ((request.HttpMethod.ToUpper() != "GET") && (request.HttpMethod.ToUpper() != "HEAD")) { response.AppendHeader("Allow", "GET, HEAD"); ResponseError(response, 405, "Method Not Allowed"); return; } string parametersString = GetParameters(request).TrimEnd('/').TrimStart('/').Replace("-", "+").Replace("_", "/"); /* Replace invalid URL chars */ parametersString = SymmCryptHelper.DecryptWithAES256FixedIV(BinaryContentUtils.DatabaseBinaryEnvironmentPassword, parametersString, Convert.FromBase64String(PRIVATE_SALT)); var parameters = parametersString.Split('/'); if (parameters.Length < 3) { ResponseError(response, 400, "Bad Request"); return; } var entity = parameters[0].ToLower(); var attribute = parameters[1].ToLower(); var id = parameters[2]; object objt = htDbBinaryConfig[entity + "/" + attribute]; if (objt == null) { ResponseError(response, 404, "Not Found"); return; } DbBinaryConfig dbBinaryConfig; dbBinaryConfig = (DbBinaryConfig)objt; byte[] buffer = GetDatabaseBinaryAttribute(id, dbBinaryConfig); if (buffer == null) { ResponseError(response, 404, "Not Found"); } else { string md5Hash = SecureHashHelper.Hash(buffer); response.Clear(); response.StatusCode = 200; //OK response.ContentType = RuntimePlatformUtils.GetMIMEType(buffer); response.AppendHeader("ETag", "\"" + md5Hash + "\""); response.AppendHeader("Accept-Ranges", "none"); if (request.HttpMethod.ToUpper() == "GET") { BinaryWrite(response, buffer); } End(response); } }
public static int GetBinaryContent(Dictionary <string, BinaryContentUtils.DbBinaryConfig> htDbBinaryConfig, string input, int userIdLogged, out byte[] content) { string parametersString = input.TrimEnd('/').TrimStart('/').Replace("-", "+").Replace("_", "/"); parametersString = SymmCryptHelper.DecryptWithAES256FixedIV(BinaryContentUtils.DatabaseBinaryEnvironmentPassword, parametersString, Convert.FromBase64String(PRIVATE_SALT)); content = null; /* * 0 => Entity's global object key * 1 => Name of the binary data attribute * 2 => ID of the record that has the binary data * 3 => User ID for which the URL was generated * 4 => An hash of the binary data (forces the URL to change when the binary data changes) */ var parameters = parametersString.Split('/'); if (parameters.Length < 4) { return((int)HttpStatusCode.BadRequest); } var entityGlobalKey = parameters[0].ToLower(); var attribute = parameters[1].ToLower(); var id = parameters[2]; var userId = 0; if (int.TryParse(parameters[3], out userId) && userId != userIdLogged) { return((int)HttpStatusCode.Unauthorized); } object objt = htDbBinaryConfig[entityGlobalKey + "/" + attribute]; if (objt == null) { return((int)HttpStatusCode.NotFound); } DbBinaryConfig dbBinaryConfig; dbBinaryConfig = (DbBinaryConfig)objt; byte[] buffer = GetDatabaseBinaryAttribute(id, dbBinaryConfig); if (buffer == null) { return((int)HttpStatusCode.NotFound); } else { content = buffer; return((int)HttpStatusCode.OK); } }