8000 于是弄好了服务器那边的avater上传 · hxdnshx/PYHHelper@158bcf9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 158bcf9

Browse files
committed
于是弄好了服务器那边的avater上传
1 parent 6b458b7 commit 158bcf9

File tree

11 files changed

+143
-27
lines changed

11 files changed

+143
-27
lines changed

HelperSrv_2/Controllers/ImgUploadController.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,58 @@ public class ImgUploadController : Controller
1717
{
1818
//懒得弄到依赖注入里面了,嘤嘤嘤
1919
private static Dictionary<string, string> pendingAvater = new Dictionary<string, string>();
20+
21+
22+
2023
[HttpPost("Upload")]
21-
public string Upload([FromForm] string UserName, [FromForm] IFormFile userAvater)
24+
public IActionResult Upload([FromForm] IFormFile file)
2225
{
26+
27+
string guid = Guid.NewGuid().ToString() + ".png";
28+
string dstPath = Path.Combine("Images", guid);
2329
try
2430
{
25-
using (var stream = userAvater.OpenReadStream())
31+
using (var stream = file.OpenReadStream())
2632
{
27-
string guid = Guid.NewGuid().ToString() + ".png";
28-
string dstPath = Path.Combine("Images", guid);
2933
using (var img = Image.Load(stream))
3034
{
3135
img.Mutate(x => x.Resize(96, 96));
32-
img.Save(Path.Combine(dstPath));
36+
img.Save(Path.Combine("wwwroot", dstPath));
3337
}
34-
pendingAvater.Add(UserName,dstPath);
3538
}
3639
}
3740
catch (Exception e)
3841
{
39-
return (new JObject {{"status", "1"},{"error", e.ToString()}}).ToString();
42+
return new ContentResult
43+
{
44+
Content = (new JObject { { "status", "1" }, { "error", e.ToString() } }).ToString() ,
45+
StatusCode = 500
46+
};
47+
}
48+
return new ContentResult
49+
{
50+
Content = (new JObject { { "status", "0" }, { "path", dstPath } }).ToString(),
51+
StatusCode = 200
52+
};
53+
}
54+
55+
[HttpPost("Add")]
56+
public string AddEntry([FromForm] string name, [FromForm] string img)
57+
{
58+
try
59+
{
60+
if (System.IO.File.Exists(Path.Combine("wwwroot", img)))
61+
pendingAvater[name] = img;
62+
else
63+
{
64+
throw new Exception("Could not found File:" + img);
65+
}
66+
}
67+
catch (Exception e)
68+
{
69+
return (new JObject { { "status", "1" }, { "error", e.ToString() } }).ToString();
4070
}
41-
return (new JObject {{ "status", "0" }}).ToString();
71+
return (new JObject { { "status", "0" } }).ToString();
4272
}
4373

4474
[HttpGet("List")]
@@ -55,6 +85,7 @@ public string List()
5585
});
5686
}
5787
obj.Add("datas", datas);
88+
pendingAvater.Clear();
5889
return obj.ToString();
5990
}
6091
}
Binary file not shown.
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
此文件由 Web 项目的发布/打包过程使用。可以通过编辑此 MSBuild 文件
4+
自定义此过程的行为。为了解与此相关的更多内容,请访问 https://go.microsoft.com/fwlink/?LinkID=208121。
5+
-->
6+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
7+
<PropertyGroup>
8+
<WebPublishMethod>FileSystem</WebPublishMethod>
9+
<PublishProvider>FileSystem</PublishProvider>
10+
<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
11+
<LastUsedPlatform>Any CPU</LastUsedPlatform>
12+
<SiteUrlToLaunchAfterPublish />
13+
<LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
14+
<ExcludeApp_Data>False</ExcludeApp_Data>
15+
<ProjectGuid>a5145ec5-c78d-4c1b-b343-b205100ba94e</ProjectGuid>
16+
<publishUrl>bin\Release\HelperSev</publishUrl>
17+
<DeleteExistingFiles>False</DeleteExistingFiles>
18+
</PropertyGroup>
19+
</Project>

HelperSrv_2/Startup.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public Startup(IConfiguration configuration)
2525
// This method gets called by the runtime. Use this method to add services to the container.
2626
public void ConfigureServices(IServiceCollection services)
2727
{
28+
services.AddCors();
2829
services.AddMvc();
2930
}
3031

@@ -41,10 +42,13 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
4142
app.UseStaticFiles(new StaticFileOptions
4243
{
4344
FileProvider = new PhysicalFileProvider(
44-
Path.Combine(Directory.GetCurrentDirectory(), "Images")),
45-
RequestPath = "/Images"
45+
Path.Combine(Directory.GetCurrentDirectory(),"wwwroot")),
46+
RequestPath = ""
4647
});
47-
48+
app.UseCors(builder =>
49+
builder
50+
.AllowAnyHeader().AllowAnyMethod()
51+
);
4852
app.UseMvc();
4953
}
5054
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<template>
2+
$END$
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: "avater-upload"
8+
}
9+
</script>
10+
11+
<style scoped>
12+
13+
</style>

PYHHelper/Form1.Designer.cs

Lines changed: 7 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PYHHelper/Form1.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Threading.Tasks;
1919
using System.Windows.Forms;
2020
using Microsoft.EntityFrameworkCore;
21+
using Newtonsoft.Json.Linq;
2122

2223
namespace PYHHelper
2324
{
@@ -43,6 +44,7 @@ int lParam // second message parameter
4344

4445
private FileSystemWatcher fsw;
4546
private HttpClient _client;
47+
private HttpClient _client2;
4648

4749
private ReplayRecord _records;
4850
private List<ReplayTable> _current;
@@ -63,7 +65,7 @@ public Form1()
6365
fsw.Filter = "*.rep";
6466
fsw.IncludeSubdirectories = true;
6567
fsw.EnableRaisingEvents = false;
66-
68+
_client2 = new HttpClient();
6769
_client = new HttpClient();
6870
_client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A356 Safari/604.1");
6971
//client.DefaultRequestHeaders.Add("Content-Type", "application /x-www-form-urlencoded");
@@ -231,9 +233,9 @@ public static byte[] Decompress_GZip(byte[] gzip)
231233
}
232234
}
233235

234-
protected string HttpGet(string uri)
236+
protected string HttpGet(string uri, HttpClient client = null)
235237
{
236-
var result = _client.GetAsync(uri).Result;
238+
var result = (client??_client).GetAsync(uri).Result;
237239
if (result.Content.Headers.ContentEncoding.Contains("gzip"))
238240
{
239241
var rawResult = result.Content.ReadAsByteArrayAsync().Result;
@@ -701,6 +703,50 @@ private void button6_Click(object sender, EventArgs e)
701703
{
702704
TH155Addr.TH155EnumRTCHild();
703705
}
706+
707+
private void timer2_Tick(object sender, EventArgs e)
708+
{
709+
var str = HttpGet("http://pyh.saigetsu.moe/api/ImgUpload/List",_client2);
710+
str = < 179B span class=pl-s1>str.Substring(1, str.Length - 2);
711+
str = str.Replace("\\n", "\n");
712+
str = str.Replace("\\\"", "\"");
713+
var obj = JObject.Parse(str);
714+
foreach (var elements in obj["datas"] as JArray)
715+
{
716+
var profileName = elements["name"].Value<string>();
717+
var imgPath = "http://pyh.saigetsu.moe/" + elements["img"].Value<string>();
718+
File.AppendAllText("AvaterLog.log", $"{profileName} {imgPath}\n");
719+
if (!IsValidFilename(profileName))
720+
continue;
721+
try
722+
{
723+
var data = _client2.GetAsync(imgPath).Result.Content.ReadAsByteArrayAsync().Result;
724+
File.WriteAllBytes($"Avatar\\{profileName}.png", data);
725+
}
726+
catch (Exception ex)
727+
{
728+
File.AppendAllText("AvaterLog.log", ex + "\n");
729+
}
730+
}
731+
}
732+
733+
//From:https://stackoverflow.com/questions/62771/how-do-i-check-if-a-given-string-is-a-legal-valid-file-name-under-windows
734+
static Regex containsABadCharacter = new Regex("["
735+
+ Regex.Escape(new string(System.IO.Path.GetInvalidPathChars())) + "]");
736+
public static bool IsValidFilename(string testName)
737+
{
738+
739+
if (containsABadCharacter.IsMatch(testName)) { return false; };
740+
741+
// other checks for UNC, drive-path format, etc
742+
743+
return true;
744+
}
745+
746+
private void Form1_Load_1(object sender, EventArgs e)
747+
{
748+
749+
}
704750
}
705751

706752
public class ReplayRecord : DbContext

PYHHelper/Form1.resx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,12 @@
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120120
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
121-
<value>17, 17</value>
121+
<value>107, 17</value>
122122
</metadata>
123123
<metadata name="openFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
124-
<value>107, 17</value>
124+
<value>197, 17</value>
125+
</metadata>
126+
<metadata name="timer2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
127+
<value>17, 17</value>
125128
</metadata>
126129
</root>

PYHHelper/PYHHelper.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@
8585
<Reference Include="Microsoft.Extensions.Primitives, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
8686
<HintPath>..\packages\Microsoft.Extensions.Primitives.2.1.1\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll</HintPath>
8787
</Reference>
88+
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
89+
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
90+
</Reference>
8891
<Reference Include="Remotion.Linq, Version=2.2.0.0, Culture=neutral, PublicKeyToken=fee00910d6e5f53b, processorArchitecture=MSIL">
8992
<HintPath>..\packages\Remotion.Linq.2.2.0\lib\net45\Remotion.Linq.dll</HintPath>
9093
</Reference>

0 commit comments

Comments
 (0)
0