-
Notifications
You must be signed in to change notification settings - Fork 3
/
Common.cs
128 lines (112 loc) · 5.73 KB
/
Common.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using Newtonsoft.Json;
using HtmlAgilityPack;
using System.Web;
using Microsoft.Azure.Storage.Blob;
using Microsoft.Data.SqlClient;
using System.Linq;
namespace HvidevoldDevelopmentENK.GetPixelArt
{
public static class Common
{
public static string BaseURI = "https://opengameart.org";
public static string FileURI = "https://opengameart.org/sites/default";
public static string SearchURI = "https://opengameart.org/art-search-advanced?keys=&title=&field_art_tags_tid_op=or&field_art_tags_tid=&name=&field_art_type_tid%5B%5D=9&field_art_licenses_tid%5B%5D=17981&field_art_licenses_tid%5B%5D=2&field_art_licenses_tid%5B%5D=17982&field_art_licenses_tid%5B%5D=3&field_art_licenses_tid%5B%5D=6&field_art_licenses_tid%5B%5D=5&field_art_licenses_tid%5B%5D=10310&field_art_licenses_tid%5B%5D=4&field_art_licenses_tid%5B%5D=8&field_art_licenses_tid%5B%5D=7&sort_by=created&sort_order=DESC&items_per_page=144&Collection=";
public static string ExtractFolder = "/extract";
public static string Container = "opengameart";
public static bool CreatedDatabase = false;
public static async Task<string> ReadURIOrCache(CloudBlockBlob blob, string uri, HttpClient client)
{
string responseBody = null;
if (await blob.ExistsAsync())
{
responseBody = await blob.DownloadTextAsync();
}
if (responseBody == null || responseBody.Length == 0)
{
// Avoid spamming server
await Task.Delay(5000);
responseBody = await client.GetStringAsync(uri);
await blob.UploadTextAsync(responseBody);
}
return responseBody;
}
public static async Task<Tuple<byte[], long>> ReadURIOrCacheBinary(CloudBlockBlob blob, string uri, HttpClient client, bool needsData = false)
{
//byte[] responseBody;
MemoryStream ms = new MemoryStream();
if (await blob.ExistsAsync() && blob.Properties.Length > 0) {
if (needsData) {
await blob.DownloadToStreamAsync(ms);
}
}
else
{
// Avoid spamming server
await Task.Delay(5000);
await blob.UploadFromStreamAsync(await client.GetStreamAsync(uri));
if (needsData) {
await blob.DownloadToStreamAsync(ms);
}
}
return Tuple.Create(ms.ToArray(), blob.Properties.Length);
}
public static async Task<bool> UpdateDatabase(string fileName, long fileSize, ILogger log) {
var connectionString = Environment.GetEnvironmentVariable("SqlDb");
//var connectionString = ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString;
int rows = 0;
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
if (!CreatedDatabase) {
var text = @"if not exists (select * from sysobjects where name='opengameartblobfiles' and xtype='U')
create table opengameartblobfiles (
id INT NOT NULL IDENTITY PRIMARY KEY,
name VARCHAR(900) NOT NULL UNIQUE,
size INT NOT NULL,
hasfeatures BIT NOT NULL,
inmilvus BIT NOT NULL
)";
using (SqlCommand cmd = new SqlCommand(text, conn))
{
// Execute the command and log the # rows affected.
var tableRows = await cmd.ExecuteNonQueryAsync();
log.LogInformation($"Create Table: {tableRows} rows were updated");
}
using (SqlCommand cmd = new SqlCommand("@CREATE INDEX inmilvus1 ON opengameartblobfiles (inmilvus);", conn)) {
// Execute the command and log the # rows affected.
var tableRows = await cmd.ExecuteNonQueryAsync();
log.LogInformation($"Create Index for Table: {tableRows} rows were updated");
}
CreatedDatabase = true;
}
var update = $"INSERT INTO opengameartblobfiles (name, size, hasfeatures, inmilvus) VALUES ('{fileName}', {fileSize}, 0, 0);";
log.LogInformation($"Trying SQL: {update}");
using (SqlCommand cmd = new SqlCommand(update, conn))
{
// Execute the command and log the # rows affected.
rows = await cmd.ExecuteNonQueryAsync();
log.LogInformation($"UpdateDatabase file: {fileName} of size {fileSize} where {rows} rows were updated.");
}
}
return rows > 0;
}
public static async Task AfterUploadFile(string fileName, long fileSize, ILogger log, ICollector<string> imgs) {
if (await UpdateDatabase(fileName, fileSize, log)) {
var isJpg = fileName.Split('.').Last().ToLower() == "png";
var isPng = fileName.Split('.').Last().ToLower() == "jpg";
if (isJpg || isPng) {
imgs.Add(fileName);
}
};
}
}
}