forked from ukparliament/Photo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds function and cli for compressing and renaming originals.
- Loading branch information
Showing
14 changed files
with
4,356 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
bin | ||
obj | ||
*.csproj.user | ||
publishprofiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace FunctionApp1 | ||
{ | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using ImageMagick; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Azure.WebJobs.Extensions.Http; | ||
using Microsoft.WindowsAzure.Storage; | ||
using Microsoft.WindowsAzure.Storage.Blob; | ||
using Newtonsoft.Json.Linq; | ||
|
||
public static class Convert | ||
{ | ||
[FunctionName("Convert")] | ||
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)]HttpRequestMessage req) | ||
{ | ||
var bodyJson = await req.Content.ReadAsAsync<JObject>(); | ||
var photoId = bodyJson["PhotoID"].Value<string>(); | ||
var imageResourceUri = bodyJson["ImageResourceUri"].Value<string>(); | ||
var storage = bodyJson["Storage"].Value<string>(); | ||
|
||
var account = CloudStorageAccount.Parse(storage); | ||
var client = account.CreateCloudBlobClient(); | ||
|
||
var original = client.GetContainerReference("originals").GetBlockBlobReference(photoId); | ||
var modified = client.GetContainerReference("photo").GetBlockBlobReference(imageResourceUri); | ||
modified.Properties.ContentType = "image/tiff"; | ||
|
||
using (var image = new MagickImage()) | ||
{ | ||
using (var stream = await original.OpenReadAsync()) | ||
{ | ||
image.Read(stream); | ||
} | ||
|
||
image.Settings.Compression = CompressionMethod.LZW; | ||
|
||
using (var stream = await modified.OpenWriteAsync()) | ||
{ | ||
image.Write(stream); | ||
} | ||
} | ||
|
||
return req.CreateResponse(HttpStatusCode.OK); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net461</TargetFramework> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Magick.NET-Q8-AnyCPU" Version="7.8.0" /> | ||
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.19" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.CSharp" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Update="host.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
</None> | ||
<None Update="local.settings.json"> | ||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
<CopyToPublishDirectory>Never</CopyToPublishDirectory> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"IsEncrypted": false, | ||
"Values": { | ||
"AzureWebJobsStorage": "", | ||
"AzureWebJobsDashboard": "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp2.1</TargetFramework> | ||
<UserSecretsId>8dbd6c4b-5f6a-4f59-ad05-fca19d0cdf63</UserSecretsId> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="SharePointExport.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="SharePointExport.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.1.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
let | ||
Site = SharePoint.Tables("https://hopuk.sharepoint.com/sites/datateam/datasources", [ApiVersion = 15]), | ||
PhotoList = Site{[Title = "Photo"]}, | ||
PhotoItems = PhotoList[Items], | ||
Filtered = Table.SelectRows(PhotoItems , each ([Image in databases] = true)), | ||
Removed = Table.SelectColumns(Filtered, {"PhotoID", "ImageResourceUri"}), | ||
Json = Json.FromValue(Removed), | ||
Text = Text.FromBinary(Json) | ||
in | ||
Text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
namespace ConsoleApp1 | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Configuration; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
internal class Program | ||
{ | ||
private static string functionUrl; | ||
private static string storage; | ||
|
||
private static void Main(string[] args) | ||
{ | ||
var config = Program.Config; | ||
Program.functionUrl = config.GetValue<string>("FunctionUrl"); | ||
Program.storage = config.GetValue<string>("Storage"); | ||
|
||
Program.Run().Wait(); | ||
} | ||
|
||
private static async Task Run() | ||
{ | ||
var config = Config; | ||
var mappings = Mappings; | ||
var functionUrl = config.GetValue<string>("FunctionUrl"); | ||
var storage = config.GetValue<string>("Storage"); | ||
|
||
Parallel.ForEach<JToken>(Mappings.Children(), new ParallelOptions { MaxDegreeOfParallelism = 50 }, X); | ||
|
||
|
||
//for (var i = 0; i < mappings.Count; i++) | ||
//{ | ||
// var mapping = mappings[i]; | ||
// mapping["Storage"] = storage; | ||
|
||
// using (var client = new HttpClient()) | ||
// { | ||
// Console.WriteLine("{0} / {1}", i, mappings.Count); | ||
// Console.WriteLine(mapping["PhotoID"]); | ||
// Console.WriteLine(mapping["ImageResourceUri"]); | ||
|
||
// var timer = Stopwatch.StartNew(); | ||
|
||
// var response = await client.PostAsync(functionUrl, new StringContent(mapping.ToString(), Encoding.UTF8, "application/json")); | ||
|
||
// Console.WriteLine(response.StatusCode); | ||
// Console.WriteLine(timer.Elapsed); | ||
// Console.WriteLine(); | ||
// } | ||
//} | ||
} | ||
|
||
private static void X(JToken mapping, ParallelLoopState arg2, long arg3) | ||
{ | ||
mapping["Storage"] = Program.storage; | ||
|
||
using (var client = new HttpClient()) | ||
{ | ||
Console.WriteLine("Start {0} ({1})", mapping["PhotoID"], arg3); | ||
|
||
var timer = Stopwatch.StartNew(); | ||
|
||
var response = client.PostAsync(Program.functionUrl, new StringContent(mapping.ToString(), Encoding.UTF8, "application/json")).Result; | ||
|
||
Console.WriteLine("Finish {0} ({1}) with {2} in {3}", mapping["PhotoID"], arg3, response.StatusCode, timer.Elapsed); | ||
} | ||
|
||
} | ||
|
||
private static IConfigurationRoot Config | ||
{ | ||
get | ||
{ | ||
var configBuilder = new ConfigurationBuilder(); | ||
|
||
configBuilder.AddUserSecrets<Program>(); | ||
|
||
return configBuilder.Build(); | ||
} | ||
} | ||
|
||
private static JArray Mappings | ||
{ | ||
get | ||
{ | ||
using (var stream = Assembly.GetEntryAssembly().GetManifestResourceStream("ConsoleApp1.SharePointExport.json")) | ||
{ | ||
using (var reader = new StreamReader(stream)) | ||
{ | ||
using (var jsonReader = new JsonTextReader(reader)) | ||
{ | ||
return JArray.Load(jsonReader); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.