-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZipQueueTrigger.cs
110 lines (93 loc) · 4.98 KB
/
ZipQueueTrigger.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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.Storage;
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 System.Collections.Generic;
using System.Linq;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using SharpCompress.Readers;
using System.Text;
namespace HvidevoldDevelopmentENK.GetPixelArt
{
public static class ZipQueueTrigger
{
public static async Task Extract(IEnumerable<IArchiveEntry> archiveEntries, string zipfile, CloudBlobContainer container, ILogger log, ICollector<string> sqls) {
string lastOutBlobName = Common.ExtractFolder + zipfile + "/" + HttpUtility.UrlEncode(archiveEntries.Last().Key, Encoding.UTF8);
var lastOutBlob = container.GetBlockBlobReference(lastOutBlobName);
if (await lastOutBlob.ExistsAsync() && lastOutBlob.Properties.Length == archiveEntries.Last().Size) {
log.LogInformation($"Last file {lastOutBlobName} already exists, so skipped ALL.");
} else {
foreach (var archiveEntry in archiveEntries.Where(entry => !entry.IsDirectory))
{
log.LogInformation($"Now processing {archiveEntry.Key}");
string outBlobName = Common.ExtractFolder + zipfile + "/" + HttpUtility.UrlEncode(archiveEntry.Key, Encoding.UTF8);
log.LogInformation($"Writing blob {outBlobName}");
NameValidator.ValidateBlobName(outBlobName);
var blockBlob = container.GetBlockBlobReference(outBlobName);
if (await blockBlob.ExistsAsync() && blockBlob.Properties.Length == archiveEntry.Size) {
log.LogInformation($"{outBlobName} already exists, so skipped.");
} else {
await using var fileStream = archiveEntry.OpenEntryStream();
await blockBlob.UploadFromStreamAsync(fileStream);
//await Common.AfterUploadFile(outBlobName, blockBlob.Properties.Length, log, imgs);
log.LogInformation($"{outBlobName} processed successfully and moved to destination container.");
}
sqls.Add(outBlobName);
}
}
}
[FunctionName("ZipQueueTrigger")]
public static async Task Run([
QueueTrigger("zipqueue", Connection = "AzureWebJobsStorage")] string zipfile,
[Blob("opengameart/{queueTrigger}")] CloudBlockBlob blob,
[StorageAccount("AzureWebJobsStorage")] CloudStorageAccount storageAccount,
[Queue("sqlqueue"), StorageAccount("AzureWebJobsStorage")] ICollector<string> sqls,
ILogger log)
{
log.LogInformation($"C# ZipQueueTrigger function processed: {zipfile}");
var isZip = zipfile.Split('.').Last().ToLower() == "zip";
var isRar = zipfile.Split('.').Last().ToLower() == "rar";
try{
if(isZip || isRar){
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(Common.Container);
await container.CreateIfNotExistsAsync();
using(MemoryStream blobMemStream = new MemoryStream()){
await blob.DownloadToStreamAsync(blobMemStream);
var zipReaderOptions = new ReaderOptions()
{
ArchiveEncoding = new ArchiveEncoding(Encoding.UTF8, Encoding.UTF8), LookForHeader = true
};
log.LogInformation("Blob is a zip/rar file; beginning extraction....");
blobMemStream.Position = 0;
if (isZip) {
using (var reader = ZipArchive.Open(blobMemStream, zipReaderOptions)) {
await Extract(reader.Entries, zipfile, container, log, sqls);
}
} else if (isRar) {
using (var reader = RarArchive.Open(blobMemStream, zipReaderOptions)) {
await Extract(reader.Entries, zipfile, container, log, sqls);
}
}
}
}
}
catch(Exception ex){
log.LogError($"Error! Something went wrong: {ex.Message}");
}
}
}
}