Permalink
Browse files

Putts' V-Day Present to Voat

100% Unit Test Coverage on Core Port!

Sql Server on Redis Cache - 100% passing
Sql Server on Memory Cache - 100% passing
PostgreSQL on Redis Cache - 100% passing
PostgreSQL on Memory Cache - 100% passing

:)
  • Loading branch information...
PuttItOut committed Jun 19, 2017
1 parent ceac398 commit 7e214804445c5f9d74759828ef2ffa22da7f8efb
@@ -28,7 +28,7 @@ protected override async Task ProcessBatch(IEnumerable<ILogInformation> batch)
}
private IEnumerable<EventLog> Map(IEnumerable<ILogInformation> info)
{
return info.Select(x => Map(x));
return info.Select(x => Map(x)).ToList();
}
private EventLog Map(ILogInformation info)
{
@@ -2836,7 +2836,7 @@ public async Task<CommandResponse<Domain.Models.Message>> SendMessage(Domain.Mod

var mappedDataMessages = messages.Map();
db.Message.AddRange(mappedDataMessages);
var addedMessages = db.Message;
var returnMessage = mappedDataMessages.First();

await db.SaveChangesAsync().ConfigureAwait(CONSTANTS.AWAIT_CAPTURE_CONTEXT);

@@ -2849,7 +2849,7 @@ public async Task<CommandResponse<Domain.Models.Message>> SendMessage(Domain.Mod
null,
message.Content));

return CommandResponse.Successful(addedMessages.First().Map());
return CommandResponse.Successful(returnMessage.Map());
}
catch (Exception ex)
{
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Voat.Common;
using Voat.Configuration;
@@ -52,12 +50,16 @@ public override string Uri(FileKey key, PathOptions options = null)
return result;

}
public override async Task Upload(FileKey key, Uri contentPath, Func<Stream, Task<Stream>> preProcessor = null)
public override async Task Upload(FileKey key, Uri contentPath, HttpResourceOptions options = null, Func<Stream, Task<Stream>> preProcessor = null)
{
throw new NotImplementedException();
//await base.Upload(key, contentPath);
}

public override Task Upload(FileKey key, Stream stream)
{
throw new NotImplementedException();
return base.Upload(key, stream);
}
protected override string ContentPath(FileType type)
{
var path = "";
@@ -1,16 +1,8 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Voat.Business.Utilities;
using Voat.Common;
using Voat.Common.Components;
using Voat.Configuration;
using Voat.Utilities;

namespace Voat.IO
@@ -41,7 +33,10 @@ public static FileManager Instance

public abstract void Delete(FileKey key);
public abstract bool Exists(FileKey key);
public abstract Task Upload(FileKey key, Uri contentPath, Func<Stream, Task<Stream>> preProcessor = null);
public abstract Task Upload(FileKey key, Uri contentPath, HttpResourceOptions options = null, Func<Stream, Task<Stream>> preProcessor = null);

public abstract Task Upload(FileKey key, Stream stream);

public abstract string Uri(FileKey key, PathOptions options = null);

}
@@ -1,21 +1,17 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Voat.Common;
using Voat.Common.Components;
using Voat.Configuration;
using Voat.Utilities;

namespace Voat.IO
{
public interface IFileManager<K>
{
Task Upload(K key, Uri contentPath, Func<Stream, Task<Stream>> preProcessor = null);
//Dear Future People: This will likely be removed
Task Upload(K key, Uri contentPath, HttpResourceOptions options = null, Func<Stream, Task<Stream>> preProcessor = null);

Task Upload(K key, Stream stream);

string Uri(K key, PathOptions options = null);

@@ -1,13 +1,7 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Voat.Business.Utilities;
using Voat.Common;
using Voat.Common.Components;
using Voat.Configuration;
@@ -57,13 +51,13 @@ public override bool Exists(FileKey key)
return File.Exists(FilePather.Instance.LocalPath(ContentPath(key.FileType), key.ID));
}

public override async Task Upload(FileKey key, Uri contentPath, Func<Stream, Task<Stream>> preProcessor = null)
public override async Task Upload(FileKey key, Uri contentPath, HttpResourceOptions options = null, Func < Stream, Task<Stream>> preProcessor = null)
{
if (Regex.IsMatch(contentPath.Scheme, "http(s)?", RegexOptions.IgnoreCase))
{
EnsureLocalDirectoryExists(key.FileType);

using (var httpResource = new HttpResource(contentPath.ToString()))
using (var httpResource = new HttpResource(contentPath.ToString(), options))
{
await httpResource.Execute();

@@ -77,24 +71,8 @@ public override async Task Upload(FileKey key, Uri contentPath, Func<Stream, Tas
{
processedStream = await preProcessor(httpResource.Stream);
}
using (var destinationStream = new FileStream(FilePather.Instance.LocalPath(ContentPath(key.FileType), key.ID), FileMode.Create, FileAccess.Write, FileShare.None, 1048576, true))
{
await processedStream.CopyToAsync(destinationStream);
}
await Upload(key, processedStream);
}
//}
// using (var httpClient = new HttpClient())
// using (var sourceStream = await httpClient.GetStreamAsync(contentPath.ToString())) {
// var processedStream = sourceStream;
// if (preProcessor != null)
// {
// processedStream = await preProcessor(sourceStream);
// }
// using (var destinationStream = new FileStream(FilePather.Instance.LocalPath(ContentPath(key.FileType), key.ID), FileMode.Create, FileAccess.Write, FileShare.None, 1048576, true))
// {
// await processedStream.CopyToAsync(destinationStream);
// }
// }
}
else if (Regex.IsMatch(contentPath.Scheme, "file", RegexOptions.IgnoreCase) || File.Exists(contentPath.ToString()))
{
@@ -107,10 +85,7 @@ public override async Task Upload(FileKey key, Uri contentPath, Func<Stream, Tas
{
processedStream = await preProcessor(sourceStream);
}
using (var destinationStream = new FileStream(FilePather.Instance.LocalPath(ContentPath(key.FileType), key.ID), FileMode.Create, FileAccess.Write, FileShare.None, 1048576, true))
{
await processedStream.CopyToAsync(destinationStream);
}
await Upload(key, processedStream);
}
}
else
@@ -128,5 +103,13 @@ public override string Uri(FileKey key, PathOptions options = null)

return VoatUrlFormatter.BuildUrlPath(null, options, (new string[] { ContentPath(key.FileType), key.ID }).ToPathParts());
}

public override async Task Upload(FileKey key, Stream stream)
{
using (var destinationStream = new FileStream(FilePather.Instance.LocalPath(ContentPath(key.FileType), key.ID), FileMode.Create, FileAccess.Write, FileShare.None, 1048576, true))
{
await stream.CopyToAsync(destinationStream);
}
}
}
}
@@ -10,40 +10,54 @@
using Voat.Common;
using Voat.Configuration;

namespace Voat.Business.Utilities
namespace Voat.Utilities
{
public class HttpResourceOptions
{
public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5);
public bool AllowAutoRedirect { get; set; } = false;

}

public class HttpResource : IDisposable
{
private Uri _uri = null;
private Uri _redirectedUri = null;
private HttpResponseMessage _response;
private MemoryStream _stream;
private TimeSpan _timeout = TimeSpan.FromSeconds(30);
private string _title = null;
private string _contentString = null;
private Uri _image = null;
private HttpResourceOptions _options = new HttpResourceOptions();

public HttpResponseMessage Response { get => _response; }
public Stream Stream { get => _stream; }
public TimeSpan Timeout { get => _timeout; set => _timeout = value; }

public Uri Uri { get => _uri; }
public Uri RedirectedUri { get => _redirectedUri; }
public bool Redirected { get => _uri != _redirectedUri; }
public HttpResourceOptions Options { get => _options; set => _options = value; }

public HttpResource(Uri uri)
public HttpResource(Uri uri, HttpResourceOptions options = null)
{
_uri = uri;
if (options != null)
{
_options = options;
}
}
public HttpResource(string uri) : this(new Uri(uri))
public HttpResource(string uri, HttpResourceOptions options = null) : this(new Uri(uri), options)
{
}
public async Task Execute(bool allowAutoRedirect = false, HttpCompletionOption options = HttpCompletionOption.ResponseContentRead)
public async Task Execute(HttpCompletionOption options = HttpCompletionOption.ResponseContentRead)
{
var handler = new HttpClientHandler() { AllowAutoRedirect = allowAutoRedirect };
var handler = new HttpClientHandler() {
AllowAutoRedirect = _options.AllowAutoRedirect
};

using (var httpClient = new HttpClient(handler))
{
httpClient.Timeout = _timeout;
httpClient.Timeout = _options.Timeout;
httpClient.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue($"Voat-OpenGraph-Parser", "2"));

_response = await httpClient.GetAsync(Uri, options);
@@ -58,6 +72,13 @@ public async Task Execute(bool allowAutoRedirect = false, HttpCompletionOption o
}
}
}
public bool IsImage
{
get
{
return Uri == Image;
}
}
public Uri Image
{
get
@@ -135,6 +156,8 @@ private string ContentString
return _contentString;
}
}


public void Dispose()
{
_response?.Dispose();
Oops, something went wrong.

0 comments on commit 7e21480

Please sign in to comment.