Skip to content

Commit

Permalink
Added badges and a pre-commit hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
ErSoul committed Jun 10, 2024
1 parent eb7c6ff commit eae4bd6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 11 deletions.
52 changes: 52 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=$(git hash-object -t tree /dev/null)
fi

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
If you know what you are doing you can disable this check using:
git config hooks.allownonascii true
EOF
exit 1
fi

dotnet format --verify-no-changes || exit 1
dotnet test dotpaste.Tests/dotpaste.Tests.csproj --no-build --verbosity normal

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --
4 changes: 2 additions & 2 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: .NET
name: dotpaste

on:
push:
Expand All @@ -25,4 +25,4 @@ jobs:
- name: Build
run: dotnet build dotpaste/dotpaste.csproj --no-restore -c Release
- name: Test
run: dotnet test dotpaste.Tests/dotpaste.Tests.csproj --no-build --verbosity normal
run: dotnet test dotpaste.Tests/dotpaste.Tests.csproj --no-build --verbosity normal -c Release
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![Repo Size](https://img.shields.io/github/repo-size/ErSoul/dotpaste?logo=github)](https://github.com/ErSoul/dotpaste)
[![Build Status](https://github.com/ErSoul/dotpaste/workflows/dotpaste/badge.svg)](https://github.com/ErSoul/dotpaste/actions)
[![Made By](https://img.shields.io/badge/Made_By-ErSoul-yellow?logo=dotnet&logoColor=yellow)](https://github.com/ErSoul)

# dotpaste

Another terminal-friendly pastebin.
Expand Down Expand Up @@ -54,4 +58,4 @@ https://prismjs.com/#supported-languages

## TODO

- Limit content's length.
- Limit content's length.
18 changes: 10 additions & 8 deletions dotpaste/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
if (!Directory.Exists(UPLOADS_PATH))
Directory.CreateDirectory(UPLOADS_PATH);

if(!Path.EndsInDirectorySeparator(UPLOADS_PATH))
if (!Path.EndsInDirectorySeparator(UPLOADS_PATH))
UPLOADS_PATH += Path.DirectorySeparatorChar;
}
catch (Exception)
Expand Down Expand Up @@ -70,7 +70,7 @@
app.Logger.LogInformation("Started clean operation on '{}'", UPLOADS_PATH);
var directory = new DirectoryInfo(UPLOADS_PATH);
foreach(var file in directory.EnumerateFiles())
foreach (var file in directory.EnumerateFiles())
{
app.Logger.LogDebug("Deleting file '{}'", file.FullName);
file.Delete();
Expand All @@ -92,22 +92,23 @@
return Results.File("index.txt", "text/plain");
}).WithName("Index");

app.MapPost("/", (HttpRequest request) => {
app.MapPost("/", (HttpRequest request) =>
{
fileID.Increment();
var currentFileURL = WebEncoders.Base64UrlEncode(BitConverter.GetBytes(fileID.Value));
var currentURL = $"{request.Scheme}://{request.Host.Value}/content/";
if (request.HasFormContentType)
{
if(request.Form.TryGetValue("content", out var content))
if (request.Form.TryGetValue("content", out var content))
{
File.WriteAllText(UPLOADS_PATH + currentFileURL, request.Form["content"]);
return userAgents.Any(check => request.Headers.UserAgent.ToString().Contains(check, StringComparison.InvariantCultureIgnoreCase)) ?
Results.Redirect(currentURL + currentFileURL) :
Results.Text(currentURL + currentFileURL);
}
if(request.Form.Files.Any() && request.Form.Files[0].Name == "content" && acceptedContentTypes.Any(ct => request.Form.Files[0].ContentType == ct))
if (request.Form.Files.Any() && request.Form.Files[0].Name == "content" && acceptedContentTypes.Any(ct => request.Form.Files[0].ContentType == ct))
{
FileStream fileStream = new(UPLOADS_PATH + currentFileURL, FileMode.CreateNew);
request.Form.Files[0].OpenReadStream().CopyTo(fileStream);
Expand All @@ -118,7 +119,7 @@
}
}
if(acceptedContentTypes.Any(ct => request.ContentType == ct))
if (acceptedContentTypes.Any(ct => request.ContentType == ct))
{
FileStream fileStream = new(UPLOADS_PATH + currentFileURL, FileMode.CreateNew);
request.BodyReader.AsStream().CopyTo(fileStream);
Expand All @@ -131,8 +132,9 @@
return Results.BadRequest("Unsupported content type.");
}).DisableAntiforgery().WithName("Post");

app.MapGet("/content/{file}", (string file, [FromQuery(Name = "lang")] string? lang) => {
if(! File.Exists(UPLOADS_PATH + file))
app.MapGet("/content/{file}", (string file, [FromQuery(Name = "lang")] string? lang) =>
{
if (!File.Exists(UPLOADS_PATH + file))
return Results.NotFound();
if (lang == null)
Expand Down

0 comments on commit eae4bd6

Please sign in to comment.