Skip to content

Instantly share code, notes, and snippets.

View scattered-code's full-sized avatar
🏠
Working from home / Living at work

Alexandru Puiu scattered-code

🏠
Working from home / Living at work
View GitHub Profile
public static IEnumerable<Entry> GetDocumentsFromDatabase(IDocumentSession session)
{
var skip = 0;
do
{
var entries = session.Query<Entry>().Where(x => !x.Deleted).OrderByDescending(x => x.DateModified).Skip(skip).Take(1024).ToList();
foreach (var entry in entries)
yield return entry;
skip += 1024;
if (entries.Count < 1024)
using System;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Web.Infrastructure
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
public void OnResourceExecuting(ResourceExecutingContext context)
{
using System;
using System.Linq;
namespace Web.Infrastructure
{
public static class RequestHelpers
{
public static string GetBoundary(string contentType)
{
var elements = contentType.Split(' ');
POST /upload HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: __atuvc=34%7C7; permanent=0; _gitlab_session=226ad8a0be43681acf38c2fab9497240; __profilin=p%3Dt; request_method=GET
Connection: keep-alive
Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266
Content-Length: 554
var boundary = RequestHelpers.GetBoundary(_httpContextAccessor.HttpContext.Request.ContentType);
var reader = new MultipartReader(boundary, _httpContextAccessor.HttpContext.Request.Body);
var section = await reader.ReadNextSectionAsync();
var fileSection = section.AsFileSection();
var originalFilename = fileSection.FileName;
var tempFilename = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.tmp");
using (var stream = new FileStream(tempFilename, FileMode.CreateNew))
{
const int chunkSize = 1024;
var buffer = new byte[chunkSize];
var bytesRead = 0;
do
{
bytesRead = await fileSection.FileStream.ReadAsync(buffer, 0, buffer.Length);
await stream.WriteAsync(buffer, 0, bytesRead);
var storageAccount = CloudStorageAccount.Parse(_configuration["MicrosoftAzureStorage:AzureStorageConnectionString"]);
var backOffPeriod = TimeSpan.FromSeconds(2);
var blobClient = storageAccount.CreateCloudBlobClient();
blobClient.DefaultRequestOptions = new BlobRequestOptions()
{
SingleBlobUploadThresholdInBytes = 1024 * 1024, //1MB, the minimum
ParallelOperationThreadCount = 1,
RetryPolicy = new ExponentialRetry(backOffPeriod, maxAttempts: 5),
};
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
// Source: https://forums.xamarin.com/discussion/180009/how-to-work-with-custom-progressable-stream-content
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace Misc
{
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;