diff --git a/src/WebServer/index.html b/src/WebServer/index.html index 328efc17..83c02851 100644 --- a/src/WebServer/index.html +++ b/src/WebServer/index.html @@ -49,26 +49,20 @@ var playing = false userInput.addEventListener("focus", event => { if (playing) MIDIjs.resume() - else MIDIjs.play('./bgm', true) + else MIDIjs.play('./bgm.mid', true) playing = true; }) userInput.addEventListener("blur", event => MIDIjs.pause()) const a = document.getElementById("downloadLink") function compileCode() { - if (!userInput.value) { - alert("No code to compile") - return - } - fetch("./compile", { + if (!userInput.value) return alert("No code to compile") + fetch("./api/compile", { method: "POST", body: userInput.value }) .then(response => response.blob()) .then(blob => { - if (blob.type == "text/plain") { - blob.text().then(alert) - return - } + if (blob.type == "text/plain") return blob.text().then(alert) if (blob.type == "application/octet-stream") { a.href = window.URL.createObjectURL(blob) a.click() diff --git a/src/WebServer/main.ps1 b/src/WebServer/main.ps1 index 9c756689..bb1bf2ca 100644 --- a/src/WebServer/main.ps1 +++ b/src/WebServer/main.ps1 @@ -18,6 +18,8 @@ The maximum number of requests per minute per IP The maximum size of the cached file .PARAMETER MaxScriptFileSize The maximum size of the script file +.PARAMETER CacheDir +The directory to store the cached files .PARAMETER Localize The language code to be used for server-side logging .PARAMETER help @@ -36,6 +38,7 @@ param ( $ReqLimitPerMin = 5, $MaxCachedFileSize = 32mb, $MaxScriptFileSize = 2mb, + $CacheDir = "$PSScriptRoot/outputs", #_if PSScript [ArgumentCompleter({ Param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) @@ -116,7 +119,7 @@ function HandleWebCompileRequest($userInput, $context) { foreach ($byte in $userInputHash) { $userInputHashStr += $byte.ToString('x2') } - $compiledExePath = "$PSScriptRoot/outputs/$userInputHashStr.bin" + $compiledExePath = "$CacheDir/$userInputHashStr.bin" #if match cached file if (Test-Path -Path $compiledExePath -ErrorAction Ignore) { $context.Response.ContentType = "application/octet-stream" @@ -126,12 +129,12 @@ function HandleWebCompileRequest($userInput, $context) { $runspace = [powershell]::Create() $runspace.RunspacePool = $runspacePool $AsyncResult = $runspace.AddScript({ - param ($userInput, $Response, $ScriptRoot, $compiledExePath, $clientIP) + param ($userInput, $Response, $ScriptRoot, $CacheDir, $compiledExePath, $clientIP) # 加载ps12exe用于处理编译请求 Import-Module $ScriptRoot/../../ps12exe.psm1 -ErrorAction Stop - New-Item -Path $ScriptRoot/outputs -ItemType Directory -Force | Out-Null + New-Item -Path $CacheDir -ItemType Directory -Force | Out-Null # 编译代码 try { @@ -156,7 +159,8 @@ function HandleWebCompileRequest($userInput, $context) { $Response.Close() }). AddArgument($userInput).AddArgument($context.Response). - AddArgument($PSScriptRoot).AddArgument($compiledExePath).AddArgument($clientIP). + AddArgument($PSScriptRoot).AddArgument($CacheDir). + AddArgument($compiledExePath).AddArgument($clientIP). BeginInvoke() $AsyncResultArray.Add(@{ AsyncHandle = $AsyncResult @@ -174,7 +178,7 @@ function HandleRequest($context) { } $RequestUrl = $RequestUrl.Substring($HostSubUrl.Length) switch ($RequestUrl) { - '/compile' { + '/api/compile' { $Reader = New-Object System.IO.StreamReader($context.Request.InputStream) $userInput = $Reader.ReadToEnd() $Reader.Close() @@ -182,7 +186,7 @@ function HandleRequest($context) { HandleWebCompileRequest $userInput $context return } - '/bgm' { + '/bgm.mid' { # midi file $context.Response.ContentType = "audio/midi" $buffer = [System.IO.File]::ReadAllBytes("$PSScriptRoot/../bin/Unravel.mid") @@ -208,7 +212,7 @@ function HandleRequest($context) { $context.Response.Close() } function AutoCacheClear { - $Cache = Get-ChildItem -Path $PSScriptRoot/outputs -ErrorAction Ignore + $Cache = Get-ChildItem -Path $CacheDir -ErrorAction Ignore if ($MaxCachedFileSize -lt ($Cache | Measure-Object -Property Length -Sum).Sum) { $Cache | Sort-Object -Property LastAccessTime -Descending | Select-Object -First $([math]::Floor($Cache.Count / 2)) | @@ -261,7 +265,7 @@ finally { # Restore Console Window Title $Host.UI.RawUI.WindowTitle = $BackUpTitle # 清空缓存 - Remove-Item $PSScriptRoot/outputs/* -Recurse -Force -ErrorAction Ignore + Remove-Item $CacheDir/* -Recurse -Force -ErrorAction Ignore } #_else #_require ps12exe diff --git a/src/locale/en-UK.ps1 b/src/locale/en-UK.ps1 index f6e2eecc..cac3e102 100644 --- a/src/locale/en-UK.ps1 +++ b/src/locale/en-UK.ps1 @@ -21,7 +21,7 @@ WebServerHelpData = @{ title = "Usage:" Usage = "Start-ps12exeWebServer [[-HostUrl] ''] [-MaxCompileThreads ''] [-MaxCompileTime ''] - [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] + [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] [-CacheDir ''] [-Localize ''] [-help]" PrarmsData = [ordered]@{ HostUrl = "The HTTP server address to register." @@ -30,6 +30,7 @@ ReqLimitPerMin = "The maximum number of requests per minute per IP." MaxCachedFileSize = "The maximum size of the cached file." MaxScriptFileSize = "The maximum size of the script file." + CacheDir = "The directory to store the cached files." Localize = "The language code to be used for server-side logging." help = "Display this help information." } diff --git a/src/locale/es-ES.ps1 b/src/locale/es-ES.ps1 index b0857c1a..60340ba5 100644 --- a/src/locale/es-ES.ps1 +++ b/src/locale/es-ES.ps1 @@ -21,7 +21,7 @@ WebServerHelpData = @{ title = "Uso:" Usage = "Start-ps12exeWebServer [[-HostUrl] ''] [-MaxCompileThreads ''] [-MaxCompileTime ''] - [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] + [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] [-CacheDir ''] [-Localize ''] [-help]" PrarmsData = [ordered]@{ HostUrl = "La dirección del servidor HTTP que se registrará." @@ -30,6 +30,7 @@ ReqLimitPerMin = "El número de solicitudes por minuto para cada IP." MaxCachedFileSize = "El tamaño maximo de archivo caché." MaxScriptFileSize = "El tamaño maximo de archivo de código." + CacheDir = "El directorio donde se almacenan los archivos caché." Localize = "El código de idioma para el registro en el lado del servidor." help = "Mostrar esta información de ayuda." } diff --git a/src/locale/hi-IN.ps1 b/src/locale/hi-IN.ps1 index 9218f4b5..de23ee1c 100644 --- a/src/locale/hi-IN.ps1 +++ b/src/locale/hi-IN.ps1 @@ -21,7 +21,7 @@ WebServerHelpData = @{ title = "उपयोग:" Usage = "Start-ps12exeWebServer [[-HostUrl] ''] [-MaxCompileThreads ''] [-MaxCompileTime ''] - [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] + [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] [-CacheDir '<पथ>'] [-Localize '<भाषा कोड>'] [-help]" PrarmsData = [ordered]@{ HostUrl = "रजिस्टर करने के लिए HTTP सर्वर पता।" @@ -30,6 +30,7 @@ ReqLimitPerMin = "फ़ाइल प्रति मिनट की अनुरोध सीमा।" MaxCachedFileSize = "अधिकतम कैश फ़ाइल का आकार।" MaxScriptFileSize = "अधिकतम स्क्रिप्ट फ़ाइल का आकार।" + CacheDir = "अधिकतम कैश फ़ाइल का डाइरेक्टरी पथ।" Localize = "सर्वर साइड रिकॉर्ड करने के लिए उपयोग किए जाने वाले भाषा कोड।" help = "इस मदद सूचना को दिखाएँ।" } diff --git a/src/locale/ja-JP.ps1 b/src/locale/ja-JP.ps1 index 45bca0da..2dbcb681 100644 --- a/src/locale/ja-JP.ps1 +++ b/src/locale/ja-JP.ps1 @@ -21,7 +21,7 @@ WebServerHelpData = @{ title = "使用方法:" Usage = "Start-ps12exeWebServer [[-HostUrl] ''] [-MaxCompileThreads ''] [-MaxCompileTime ''] - [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] + [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] [-CacheDir '<パス>'] [-Localize '<言語コード>'] [-help]" PrarmsData = [ordered]@{ HostUrl = "登録するHTTPサーバーのアドレス。" @@ -30,6 +30,7 @@ ReqLimitPerMin = "IPアドレスごとの1分間のリクエスト制限。" MaxCachedFileSize = "最大キャッシュファイルサイズ。" MaxScriptFileSize = "最大スクリプトファイルサイズ。" + CacheDir = "キャッシュディレクトリ。" Localize = "サーバー側の記録に使用する言語コード。" help = "このヘルプ情報を表示します。" } diff --git a/src/locale/zh-CN.ps1 b/src/locale/zh-CN.ps1 index e0b0393f..382ba983 100644 --- a/src/locale/zh-CN.ps1 +++ b/src/locale/zh-CN.ps1 @@ -21,7 +21,7 @@ WebServerHelpData = @{ title = "用法:" Usage = "Start-ps12exeWebServer [[-HostUrl] ''] [-MaxCompileThreads ''] [-MaxCompileTime ''] - [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] + [-ReqLimitPerMin ''] [-MaxCachedFileSize ''] [-MaxScriptFileSize ''] [-CacheDir '<路径>'] [-Localize '<语言代码>'] [-help]" PrarmsData = [ordered]@{ HostUrl = "要注册的 HTTP 服务器地址。" @@ -30,6 +30,7 @@ ReqLimitPerMin = "每个IP每分钟的请求限制。" MaxCachedFileSize = "最大缓存文件大小。" MaxScriptFileSize = "最大脚本文件大小。" + CacheDir = "缓存目录。" Localize = "服务器端记录要使用的语言代码。" help = "显示此帮助信息。" }