Fix plain view, beautify m3u support

This commit is contained in:
Laura Hausmann 2019-08-12 20:16:33 +02:00
parent 6b061eceef
commit 928dec02e4
No known key found for this signature in database
GPG key ID: 1720DF2EC8974C2D
4 changed files with 35 additions and 77 deletions

View file

@ -3,29 +3,45 @@
@model IndexModel
@{
ViewData["Title"] = "webmusic";
var path = HttpUtility.UrlDecode(Request.QueryString.Value.TrimStart('?'));
if (path.EndsWith(".m3u"))
{
Layout = null;
Response.ContentType = "text/plain";
}
}
@if (Model.path.Contains(".."))
{
return;
}
@if (path.EndsWith(".m3u"))
{
@foreach (var file in Model.files)
{
@Html.Raw(Request.Scheme + "://" + Request.Host + "/" + Model.fullpath + "/" + file+"\n")
}
}
else
{
<h2>@Model.displaypath <span id="state"></span> <span id="flags"></span></h2>
<a class="action-muted">[..]</a>
<a href="?@Model.path_oneup" class="entry-muted cfont"> Go back</a><br/>
<a class="action-muted">[--]</a>
<a href="#" onclick="downloadm3u()" class="entry-muted cfont"> Download m3u</a><br/><br/>
@foreach (var dir in Model.dirs)
{
<a class="action" href="#">[--]</a>
<a href="?@Model.path/@dir"> @dir</a>
<br/>
}
@foreach (var file in Model.files)
{
var jspath = Model.Encode(Model.fullpath);
var jsfile = jspath + "/" + Model.Encode(file);
<script>queue.push('@Html.Raw(jsfile)')</script>
<a class="action" href="@Html.Raw(Model.Encode(Model.fullpath+"/"+file))" download>[DL]</a>
<a href="#" onclick="playSong('@Html.Raw(jsfile)')"> @file</a>
<br/>
<a href="/?@(Request.Query.FirstOrDefault().Key).m3u" class="entry-muted cfont"> Download m3u</a><br/><br/>
@foreach (var dir in Model.dirs)
{
<a class="action" href="#">[--]</a>
<a href="?@Model.path/@dir"> @dir</a>
<br/>
}
@foreach (var file in Model.files)
{
var jspath = Model.Encode(Model.fullpath);
var jsfile = jspath + "/" + Model.Encode(file);
<script>queue.push('@Html.Raw(jsfile)')</script>
<a class="action" href="@Html.Raw(Model.Encode(Model.fullpath+"/"+file))" download>[DL]</a>
<a href="#" onclick="playSong('@Html.Raw(jsfile)')"> @file</a>
<br/>
}
}

View file

@ -22,6 +22,8 @@ namespace webmusic.Pages
{
if (Request.QueryString.HasValue)
path = HttpUtility.UrlDecode(Request.QueryString.Value.Remove(0,1));
if (path.EndsWith(".m3u"))
path = path.Substring(0, path.Length - 4);
if (path.Contains(".."))
{
Response.Redirect("/Error");

View file

@ -3,6 +3,7 @@
@model IndexModel
@{
Layout = null;
Response.ContentType = "text/plain";
}
@if (Model.path.Contains(".."))
{

View file

@ -1,61 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace webmusic.Pages
{
public class PlainModel : PageModel
{
public static string root = "music";
public string path = "";
public string displaypath = "";
public string path_oneup = "";
public string fullpath = "";
public List<string> dirs = new List<string>();
public List<string> files = new List<string>();
public void OnGet()
{
if (Request.QueryString.HasValue)
path = HttpUtility.UrlDecode(Request.QueryString.Value.Remove(0,1));
if (path.Contains(".."))
{
Response.Redirect("/Error");
return;
}
path_oneup = Regex.Match(path, @".*(?=\/)").Value;
fullpath = root + path;
displaypath = string.IsNullOrWhiteSpace(path) ? "/" : path;
dirs = Directory.GetDirectories(fullpath).Select(Path.GetFileName).ToList();
dirs.Sort();
files = Directory.GetFiles(fullpath).Select(Path.GetFileName).ToList();
files.RemoveAll(p => p.EndsWith(".m3u"));
files.Sort(new NumericComparer());
}
public class NumericComparer : IComparer<string>
{
public int Compare(string x, string y)
{
var regex = new Regex(@"^\d+");
// run the regex on both strings
var xRegexResult = regex.Match(x);
var yRegexResult = regex.Match(y);
// check if they are both numbers
if (xRegexResult.Success && yRegexResult.Success)
{
return int.Parse(xRegexResult.Value).CompareTo(int.Parse(yRegexResult.Value));
}
// otherwise return as string comparison
return string.Compare(x, y, StringComparison.Ordinal);
}
}
}
}