public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "api/upload")] HttpRequest req, ILogger log) { string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic data = JsonConvert.DeserializeObject(requestBody); string id = data?.id; if (await _service.FindAsync(id) != null) { return(new ConflictObjectResult(new { })); } var media = await _service.AddAsync(id); return(new CreatedResult($"/api/medias/{Uri.EscapeUriString(id)}", new { media.DeleteToken })); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "api/medias/{id?}")] HttpRequest req, string id, ILogger log) { req.HttpContext.Response.Headers["Cache-Control"] = "no-cache"; if (string.IsNullOrEmpty(id)) { var skipToken = req.Query["skipToken"]; var medias = await _service.ListAsync(skipToken); return(new OkObjectResult(medias)); } else { var media = await _service.FindAsync(id); return(new OkObjectResult(media)); } }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "api/delete/{id}")] HttpRequest req, string id, ILogger log) { var token = req.Query["token"]; var media = await _service.FindAsync(id); if (media == null) { return(new NotFoundResult()); } if (media.DeleteToken != token) { return(new ForbidResult()); } await _service.RemoveAsync(media); return(new NoContentResult()); }
//[Singleton] public async Task Run([QueueTrigger("video-%Meeemories:ContainerName%")] string id, ILogger log) { var media = await _service.FindAsync(id); if (media == null) { log.LogInformation($"NotFound {id}"); return; } media.Status = MediaStatus.Converting; await _service.UpdateAsync(media); var rawPath = Path.GetTempFileName(); var jpgPath = $"{rawPath}.jpg"; var thumbPath = $"{rawPath}_thumb.jpg"; var resizedPath = $"{rawPath}_resized.mp4"; try { var raw = _service.OpenBlob(media); await raw.DownloadToFileAsync(rawPath, FileMode.OpenOrCreate); var ffmpeg = new Engine(StaticFiles.Path("ffmpeg.exe")); var inputFile = new MediaFile(rawPath); var jpgFile = new MediaFile(jpgPath); var thumbFile = new MediaFile(thumbPath); var resizedFile = new MediaFile(resizedPath); await ffmpeg.GetThumbnailAsync(inputFile, jpgFile, new ConversionOptions { Seek = TimeSpan.FromMilliseconds(10) }); double aspect; using (var image = new MagickImage(jpgPath)) { aspect = (double)image.Height / image.Width; } int width = 400, height = (int)(aspect * width); if (height % 2 == 1) { height += 1; } await ffmpeg.ConvertAsync(inputFile, resizedFile, new ConversionOptions { CustomWidth = width, CustomHeight = height, VideoSize = FFmpeg.NET.Enums.VideoSize.Custom, }); await ffmpeg.GetThumbnailAsync(inputFile, thumbFile, new ConversionOptions { Seek = TimeSpan.FromSeconds(3) }); var sources = new List <MediaSource>(); var thumbBlob = _service.CreateBlob($"{width:000}w/{raw.Name}.jpg"); var resizedBlob = _service.CreateBlob($"{width:000}w/{raw.Name}.mp4"); await thumbBlob.UploadFromFileAsync(thumbPath); thumbBlob.Properties.ContentType = "image/jpeg"; await thumbBlob.SetPropertiesAsync(); sources.Add(new MediaSource { Url = thumbBlob.Uri.ToString(), Width = width, Height = height, MimeType = "image/jpeg" }); await resizedBlob.UploadFromFileAsync(resizedPath); resizedBlob.Properties.ContentType = "video/mp4"; await resizedBlob.SetPropertiesAsync(); sources.Add(new MediaSource { Url = resizedBlob.Uri.ToString(), Width = width, Height = height, MimeType = "video/mp4" }); media.Sources = sources; media.Status = MediaStatus.Complete; await _service.UpdateAsync(media); } catch (Exception e) { log.LogError(e.Message); media.Status = MediaStatus.Fail; await _service.UpdateAsync(media); } finally { if (File.Exists(rawPath)) { File.Delete(rawPath); } if (File.Exists(jpgPath)) { File.Delete(jpgPath); } if (File.Exists(thumbPath)) { File.Delete(thumbPath); } if (File.Exists(resizedPath)) { File.Delete(resizedPath); } } }
public async Task Run([QueueTrigger("image-%Meeemories:ContainerName%")] string id, ILogger log) { var media = await _service.FindAsync(id); if (media == null) { log.LogInformation($"NotFound {id})"); return; } media.Status = MediaStatus.Converting; await _service.UpdateAsync(media); var raw = _service.OpenBlob(media); var rawPath = Path.GetTempFileName(); await raw.DownloadToFileAsync(rawPath, FileMode.OpenOrCreate); async Task <MediaSource> Convert(Media media, int width) { var blob = _service.CreateBlob($"{width:000}w/{raw.Name}.jpg"); using var stream = File.OpenRead(rawPath); using var image = new MagickImage(stream); var aspect = (double)image.Height / image.Width; var height = (int)(aspect * width); image.Resize(width, height); image.Format = MagickFormat.Jpeg; image.Quality = 85; var binary = image.ToByteArray(); await blob.UploadFromByteArrayAsync(binary, 0, binary.Length); blob.Properties.ContentType = "image/jpeg"; await blob.SetPropertiesAsync(); return(new MediaSource { Url = blob.Uri.ToString(), Width = width, Height = height, MimeType = "image/jpeg" }); } try { var sources = new List <MediaSource>(); sources.Add(await Convert(media, 20)); sources.Add(await Convert(media, 200)); sources.Add(await Convert(media, 400)); sources.Add(await Convert(media, 800)); media.Sources = sources; media.Status = MediaStatus.Complete; await _service.UpdateAsync(media); } catch (Exception e) { log.LogError(e.Message); media.Status = MediaStatus.Fail; await _service.UpdateAsync(media); } }