-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateZipPackageV2.cs
154 lines (119 loc) · 5.2 KB
/
CreateZipPackageV2.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 Newtonsoft.Json;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using ICSharpCode.SharpZipLib.Zip;
using Newtonsoft.Json.Linq;
using ZipFunction.util;
namespace CompressAzureBlobStorageV2
{
public static class CreateZipPackageV2
{
[FunctionName("CreateZipPackageV2")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
BlobClient zipblob;
string containerpath;
string zipfilename;
bool validatezip;
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
if (string.IsNullOrEmpty(requestBody))
{
return new BadRequestObjectResult("Body cannot be blank");
}
if(!SchemaValidator.validateZipFileRequestBody(requestBody))
{
return new BadRequestObjectResult("Schema validation failed. A valid example is: \n" +
SchemaValidator.zipFileRequestBodyExample());
}
dynamic data = JsonConvert.DeserializeObject(requestBody);
containerpath = data?.containerpath;
if (string.IsNullOrEmpty(containerpath))
{
return new BadRequestObjectResult("containerpath value cannot be blank");
}
zipfilename = data?.zipfilename;
if (string.IsNullOrEmpty(zipfilename))
{
return new BadRequestObjectResult("zipfilename value cannot be blank");
}
validatezip = data?.validatezip;
JArray filelist = JArray.Parse(@data["filelist"].ToString());
if(filelist.Count == 0)
{
return new BadRequestObjectResult("filelist count needs to be major than 0");
}
try
{
await BlobUtil.CreateBlob(zipfilename, "", containerpath, log);
zipblob = BlobUtil.getBlob(zipfilename, containerpath, log);
await zipFiles(zipblob, containerpath, filelist);
if(validatezip)
{
if(await PackageChecker.validatePackageAsync(containerpath, zipfilename))
{
return new OkObjectResult("");
}
else
{
return new BadRequestObjectResult("The zip package did not contain any data file");
}
}
return new OkObjectResult("");
}
catch (Exception e)
{
return new BadRequestObjectResult("An unhandled exception has ocurred: " + e.Message);
}
}
/// <summary>
/// Create a .zip file containing all the files in blobFileNames
/// </summary>
/// <param name="zipblob">Instance to a BlobClient</param>
/// <param name="containerpath">the path to the container where the files are</param>
/// <param name="blobFileNames">A json array containing the list of files to add to the .zip file</param>
/// <returns></returns>
public static async Task zipFiles(BlobClient zipblob, string containerpath, JArray blobFileNames)
{
var container = BlobUtil.getBlobContainer(containerpath);
MemoryStream tmpzipstream = new MemoryStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(tmpzipstream);
foreach (string blobFileName in blobFileNames)
{
string blobFileNameOrig, blobFileNameDest;
blobFileNameOrig = blobFileName.Split(',')[0].Replace("/" + containerpath + "/", "");
if (blobFileName.Contains(","))
blobFileNameDest = blobFileName.Split(',')[1];
else
blobFileNameDest = blobFileNameOrig;
zipOutputStream.SetLevel(0);
var blob = container.GetBlobClient(blobFileNameOrig);
// skip Lenght 0 files
BlobProperties properties = await blob.GetPropertiesAsync();
if (properties.ContentLength <= 0)
{
continue;
}
var entry = new ZipEntry(blobFileNameDest)
{
DateTime = DateTime.Now
};
zipOutputStream.PutNextEntry(entry);
await blob.DownloadToAsync(zipOutputStream);
}
zipOutputStream.Finish();
tmpzipstream.Seek(0, SeekOrigin.Begin);
await zipblob.UploadAsync(tmpzipstream);
zipOutputStream.Close();
}
}
}