Skip to content

Commit

Permalink
Adds function and cli for compressing and renaming originals.
Browse files Browse the repository at this point in the history
  • Loading branch information
langsamu committed Sep 23, 2018
1 parent 8ad4d0a commit a129898
Show file tree
Hide file tree
Showing 14 changed files with 4,356 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
bin
obj
*.csproj.user
publishprofiles
49 changes: 49 additions & 0 deletions Convert/Convert.cs
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);
}
}
}
21 changes: 21 additions & 0 deletions Convert/Convert.csproj
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>
2 changes: 2 additions & 0 deletions Convert/host.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
7 changes: 7 additions & 0 deletions Convert/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": ""
}
}
24 changes: 24 additions & 0 deletions ConvertAll/ConvertAll.csproj
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>
10 changes: 10 additions & 0 deletions ConvertAll/GetSharePointData.m
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
106 changes: 106 additions & 0 deletions ConvertAll/Program.cs
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);
}
}
}
}
}
}
}
Loading

0 comments on commit a129898

Please sign in to comment.