-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from lapulpeta/49-upload-media
49 upload media
- Loading branch information
Showing
15 changed files
with
235 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Nostrid.Externals | ||
{ | ||
public interface IMediaService | ||
{ | ||
public string Name { get; } | ||
|
||
public int MaxSize { get; } | ||
|
||
public Task<Uri?> UploadFile(Stream data, string filename, string mimeType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Nostrid.Externals | ||
{ | ||
public class MediaServiceProvider | ||
{ | ||
private List<IMediaService> _mediaServices = new(); | ||
|
||
public MediaServiceProvider(IEnumerable<IMediaService> mediaServices) | ||
{ | ||
_mediaServices = new(mediaServices); | ||
} | ||
|
||
public void RegisterMediaService(IMediaService service) | ||
{ | ||
_mediaServices.Add(service); | ||
} | ||
|
||
public IEnumerable<IMediaService> GetMediaServices() | ||
{ | ||
return _mediaServices.AsReadOnly(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System.Net.Http.Headers; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Nostrid.Externals | ||
{ | ||
public partial class NostrBuildMediaService : IMediaService | ||
{ | ||
private static Regex _linkRegex = LinkRegex(); | ||
|
||
public string Name => "nostr.build"; | ||
|
||
public int MaxSize { get => 50 * 1024 * 1024; } | ||
|
||
public async Task<Uri?> UploadFile(Stream data, string filename, string mimeType) | ||
{ | ||
try | ||
{ | ||
using var httpClient = new HttpClient(); | ||
using var httpContent = new MultipartFormDataContent(); | ||
using var fileContent = new StreamContent(data); | ||
fileContent.Headers.ContentType = new MediaTypeHeaderValue(mimeType); | ||
httpContent.Add(fileContent, "fileToUpload", filename); | ||
httpContent.Add(new StringContent("Upload Image"), "submit"); | ||
var response = await httpClient.PostAsync("https://nostr.build/upload.php", httpContent); | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
var responseText = await response.Content.ReadAsStringAsync(); | ||
if (responseText.IsNotNullOrEmpty()) | ||
{ | ||
var match = _linkRegex.Match(responseText); | ||
if (match.Success) | ||
{ | ||
var link = match.Groups["link"].Value; | ||
if (Uri.IsWellFormedUriString(link, UriKind.Absolute)) | ||
{ | ||
return new Uri(link); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
} | ||
return null; | ||
} | ||
|
||
[GeneratedRegex(".*(?<link>https://nostr.build/i/nostr.build_([0-9a-f]+).([0-9a-zA-Z]+)).*")] | ||
private static partial Regex LinkRegex(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.IO; | ||
using System.Net.Http.Headers; | ||
using System.Security.Cryptography; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Nostrid.Externals | ||
{ | ||
public partial class VoidCatMediaService : IMediaService | ||
{ | ||
private static Regex _linkRegex = LinkRegex(); | ||
|
||
public string Name => "void.cat"; | ||
|
||
public int MaxSize { get => 50 * 1024 * 1024; } | ||
|
||
public async Task<Uri?> UploadFile(Stream data, string filename, string mimeType) | ||
{ | ||
try | ||
{ | ||
var sha256 = Convert.ToHexString(SHA256.HashData(data)); | ||
data.Position = 0; | ||
|
||
using var httpClient = new HttpClient(); | ||
using var fileContent = new StreamContent(data); | ||
fileContent.Headers.Add("V-Full-Digest", sha256); | ||
fileContent.Headers.Add("V-Filename", filename); | ||
fileContent.Headers.ContentType = new MediaTypeHeaderValue(mimeType); | ||
var response = await httpClient.PostAsync("https://void.cat/upload?cli=true", fileContent); | ||
|
||
if (response.IsSuccessStatusCode) | ||
{ | ||
var responseText = await response.Content.ReadAsStringAsync(); | ||
if (responseText.IsNotNullOrEmpty()) | ||
{ | ||
var match = _linkRegex.Match(responseText); | ||
if (match.Success) | ||
{ | ||
var link = $"https://{match.Groups["link"].Value}"; | ||
if (Uri.IsWellFormedUriString(link, UriKind.Absolute)) | ||
{ | ||
return new Uri(link); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
|
||
} | ||
return null; | ||
} | ||
|
||
[GeneratedRegex("(http(s?):\\/\\/(?<link>[^ ]*))")] | ||
private static partial Regex LinkRegex(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,13 @@ | |
"js/bootstrap.js", | ||
"js/bootstrap.min.js" | ||
] | ||
}, | ||
{ | ||
"library": "[email protected]", | ||
"destination": "wwwroot/lib/popper.js/", | ||
"files": [ | ||
"umd/popper.min.js" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"version": "1.0", | ||
"defaultProvider": "cdnjs", | ||
"libraries": [ | ||
{ | ||
"library": "[email protected]", | ||
"destination": "wwwroot/lib/popper.js/", | ||
"files": [ | ||
"umd/popper.min.js" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.