public override bool SaveFile(HttpContext context, BlobAdapter ba, string keyValue) { if (context.Request.Files.Count != 1) { return(false); } HttpPostedFile file = context.Request.Files[0]; string p = AnnotationPlugIn.GenerateDataRecordPath(); if (!(Directory.Exists(p))) { Directory.CreateDirectory(p); } // u|OrderDetails,_Annotation_AttachmentNew|10248|11 Match m = Regex.Match(this.Value, "_Annotation_Attachment(\\w+)\\|"); if (m.Success) { string fileName = m.Groups[1].Value; if (fileName == "New") { fileName = DateTime.Now.ToString("u"); fileName = Regex.Replace(fileName, "[\\W]", String.Empty); } fileName = Path.Combine(p, (fileName + ".xml")); if (file.ContentLength == 0) { foreach (string f in Directory.GetFiles(p, (Path.GetFileNameWithoutExtension(fileName) + "*.*"))) { System.IO.File.Delete(f); } } else { XmlWriterSettings settings = new XmlWriterSettings(); settings.CloseOutput = true; XmlWriter writer = XmlWriter.Create(new FileStream(fileName, FileMode.Create), settings); try { writer.WriteStartElement("attachment"); writer.WriteAttributeString("timestamp", DateTime.Now.ToString("o")); writer.WriteAttributeString("username", HttpContext.Current.User.Identity.Name); writer.WriteAttributeString("email", AnnotationPlugIn.UserEmail); writer.WriteAttributeString("fileName", Path.GetFileName(file.FileName)); writer.WriteAttributeString("contentType", file.ContentType); writer.WriteAttributeString("contentLength", file.ContentLength.ToString()); writer.WriteAttributeString("value", Regex.Replace(this.Value, "^.+?\\|([\\w,]+?)_Annotation_Attachment(New|\\w+)(.+)$", String.Format("1|$1_Annotation_Attachment{0}$3", Path.GetFileNameWithoutExtension(fileName)))); writer.WriteEndElement(); fileName = ((Path.GetFileNameWithoutExtension(fileName) + "_") + Path.GetExtension(file.FileName)); file.SaveAs(Path.Combine(p, fileName)); } finally { writer.Close(); } } } return(true); }
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { BlobAdapter blobAdapter = new BlobAdapter(); blobAdapter.Init(); BlobsToFixQueueAdapter blobsToFixQueueAdapter = new BlobsToFixQueueAdapter(); blobsToFixQueueAdapter.Init(); Dictionary <string, List <string> > blobsByContainer = blobAdapter.GetBlobsByContainerMissingContentType(); const int chunkSize = 500; foreach (KeyValuePair <string, List <string> > keyValuePair in blobsByContainer) { for (int i = 0; i < keyValuePair.Value.Count; i += chunkSize) { List <string> blobs = keyValuePair.Value.GetRange(i, Math.Min(chunkSize, keyValuePair.Value.Count - i)); blobsToFixQueueAdapter.SendBlobsToFix(new BlobsToFix { Container = keyValuePair.Key, BlobNames = blobs }); log.Info($"Queued blobs {i} to {i + blobs.Count} in container {keyValuePair.Key}"); } } return(req.CreateResponse(HttpStatusCode.OK, "Ok")); }
public void TestBlobAdapter() { Adapter adapter = new BlobAdapter(); Entity entity = folder.SetAdapter(adapter).Get().Result; Assert.NotNull(entity); Assert.True(entity is Blob); Assert.True(IOHelper.AreFilesEqual("Puppy.docx", ((Blob)entity).File.FullName)); }
public static HttpResponseMessage Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, TraceWriter log) { BlobAdapter blobAdapter = new BlobAdapter(); blobAdapter.Init(); blobAdapter.FixServiceProperties(); return(req.CreateResponse(HttpStatusCode.OK, "Service properties still todo")); }
public override bool SaveFile(HttpContext context, BlobAdapter ba, string keyValue) { if (context.Request.Files.Count != 1) return false; HttpPostedFile file = context.Request.Files[0]; string p = AnnotationPlugIn.GenerateDataRecordPath(); if (!(Directory.Exists(p))) Directory.CreateDirectory(p); // u|OrderDetails,_Annotation_AttachmentNew|10248|11 Match m = Regex.Match(this.Value, "_Annotation_Attachment(\\w+)\\|"); if (m.Success) { string fileName = m.Groups[1].Value; if (fileName == "New") { fileName = DateTime.Now.ToString("u"); fileName = Regex.Replace(fileName, "[\\W]", String.Empty); if (System.IO.File.Exists(Path.Combine(p, (fileName + ".xml")))) fileName = (fileName + "_"); } fileName = Path.Combine(p, (fileName + ".xml")); if (file.ContentLength == 0) foreach (string f in Directory.GetFiles(p, (Path.GetFileNameWithoutExtension(fileName) + "*.*"))) System.IO.File.Delete(f); else { XmlWriterSettings settings = new XmlWriterSettings(); settings.CloseOutput = true; XmlWriter writer = XmlWriter.Create(new FileStream(fileName, FileMode.Create), settings); try { writer.WriteStartElement("attachment"); writer.WriteAttributeString("timestamp", DateTime.Now.ToString("o")); writer.WriteAttributeString("username", HttpContext.Current.User.Identity.Name); writer.WriteAttributeString("email", AnnotationPlugIn.UserEmail); writer.WriteAttributeString("fileName", Path.GetFileName(file.FileName)); writer.WriteAttributeString("contentType", file.ContentType); writer.WriteAttributeString("contentLength", file.ContentLength.ToString()); writer.WriteAttributeString("value", Regex.Replace(this.Value, "^.+?\\|([\\w,]+?)_Annotation_Attachment(New|\\w+)(.+)$", String.Format("1|$1_Annotation_Attachment{0}$3", Path.GetFileNameWithoutExtension(fileName)))); writer.WriteEndElement(); fileName = ((Path.GetFileNameWithoutExtension(fileName) + "_") + Path.GetExtension(file.FileName)); file.SaveAs(Path.Combine(p, fileName)); } finally { writer.Close(); } } } return true; }
public override bool ValidateBlobAccess(HttpContext context, BlobHandlerInfo handler, BlobAdapter ba, string val) { //* se usa código custom solo si el Override se ejecuta por los Handlers que necesitamos. if (handler.Key == "Administracion_GastoPicture" || handler.Key == "Administracion_GastoPicture2" || handler.Key == "Nombre_De_Handler_A_Usar") { //* Validar que se haya iniciado sesión y usamos una llave de validación propia de APP Framework de COT (?) No estoy seguro de que hace... var key = context.Request.Params["_validationKey"]; if (((ba == null) || !ba.IsPublic) && (!context.User.Identity.IsAuthenticated && key != BlobAdapter.ValidationKey)) { return(!ApplicationServicesBase.AuthorizationIsSupported); } else { //* Grant ACCESS. return(true); } } //* Por último, si no se concuerda con los Handlers a utilizar se llama al código original del método ValidateBlobAccess, para estar más seguros. else { return(base.ValidateBlobAccess(context, handler, ba, val)); } }
public static async Task Run([QueueTrigger(Constants.VideosToDownloadQueueName, Connection = "AzureWebJobsStorage")] string myQueueItem, TraceWriter log) { Startup.Init(); log.Info($"C# Queue trigger function processed: {myQueueItem}"); VideosToDownload videosToDownload = JsonConvert.DeserializeObject <VideosToDownload>(myQueueItem); BlobAdapter blobAdapter = new BlobAdapter(); blobAdapter.Init(); VideoIndexTableAdapter videoIndexTableAdapter = new VideoIndexTableAdapter(); videoIndexTableAdapter.Init(); PostsTableAdapter postsTableAdapter = new PostsTableAdapter(); postsTableAdapter.Init(log); ReversePostsTableAdapter reversePostsTableAdapter = new ReversePostsTableAdapter(); reversePostsTableAdapter.Init(log); string sourceBlog = string.IsNullOrEmpty(videosToDownload.SourceBlog) ? videosToDownload.IndexInfo.BlogName : videosToDownload.SourceBlog; sourceBlog = SanityHelper.SanitizeSourceBlog(sourceBlog); using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("video/*")); httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/*")); List <Video> videos = new List <Video>(); string blogname = videosToDownload.IndexInfo.BlogName; string id = videosToDownload.IndexInfo.PostId; DateTime date = videosToDownload.IndexInfo.PostDate; foreach (VideoUrls videoUrls in videosToDownload.VideoUrls) { try { Video blobVideo = await blobAdapter.HandleVideo(videoUrls, videosToDownload.IndexInfo.BlogName, log); videos.Add(blobVideo); videoIndexTableAdapter.InsertVideoIndex(blogname, id, date, blobVideo, videosToDownload.VideoType, blobVideo.Bytes, videosToDownload.Duration); log.Info("Video successfully downloaded: " + videoUrls.VideoUrl); } catch (HttpRequestException ex) { log.Warning("HTTP Error while downloading video " + videoUrls.VideoUrl + " - " + ex.Message); postsTableAdapter.MarkWithVideoDownloadError(blogname, id, ex.Message); } catch (Exception ex) { log.Error("Error while downloading video ", ex); throw; } } if (videos.Count > 0) { postsTableAdapter.MarkVideosAsDownloaded(videosToDownload.IndexInfo.BlogName, videosToDownload.IndexInfo.PostId, videos.ToArray()); ReversePostEntity reversePost = new ReversePostEntity(blogname, id, videosToDownload.PostType, date, videosToDownload.Body, videosToDownload.Title) { Videos = JsonConvert.SerializeObject(videos) }; reversePostsTableAdapter.InsertPost(reversePost); } } }
public static async Task Run([QueueTrigger(Constants.PhotosToDownloadQueueName, Connection = "AzureWebJobsStorage")] string myQueueItem, TraceWriter log) { Startup.Init(); string requestUrl = null; try { PhotosToDownload photosToDownload = JsonConvert.DeserializeObject <PhotosToDownload>(myQueueItem); BlobAdapter blobAdapter = new BlobAdapter(); blobAdapter.Init(); PhotoIndexTableAdapter photoIndexTableAdapter = new PhotoIndexTableAdapter(); photoIndexTableAdapter.Init(); PostsTableAdapter postsTableAdapter = new PostsTableAdapter(); postsTableAdapter.Init(log); ReversePostsTableAdapter reversePostsTableAdapter = new ReversePostsTableAdapter(); reversePostsTableAdapter.Init(log); List <Photo> sitePhotos = new List <Photo>(); string blogname = photosToDownload.IndexInfo.BlogName; string id = photosToDownload.IndexInfo.PostId; DateTime date = photosToDownload.IndexInfo.PostDate; string sourceBlog = string.IsNullOrEmpty(photosToDownload.SourceBlog) ? photosToDownload.IndexInfo.BlogName : photosToDownload.SourceBlog; sourceBlog = SanityHelper.SanitizeSourceBlog(sourceBlog); using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/*")); foreach (Model.Tumblr.Photo photo in photosToDownload.Photos) { bool isOriginal = true; Photo sitePhoto = null; foreach (AltSize altSize in photo.Alt_sizes) { PhotoUrlHelper urlHelper = PhotoUrlHelper.ParseTumblr(altSize.Url); if (isOriginal || urlHelper != null && DownloadSizes.Contains(urlHelper.Size)) { if (sitePhoto == null) { sitePhoto = new Photo { Name = urlHelper.Container + "_" + urlHelper.Name, Extension = urlHelper.Extension, Sizes = new PhotoSize[0] } } ; PhotoUrlIndexEntity urlIndexEntity = photoIndexTableAdapter.GetPhotoUrlndex(sourceBlog, altSize.Url); if (urlIndexEntity != null) // photo already downloaded { AddSizeToSitePhoto(sitePhoto, urlIndexEntity.BlobUrl, altSize); // need this to produce correct sitePhotos isOriginal = false; } else // photo not downloaded { requestUrl = altSize.Url; byte[] photoBytes = await httpClient.GetByteArrayAsync(altSize.Url); if (photoBytes.Length > 0) { Uri blobUri = await blobAdapter.UploadPhotoBlob(urlHelper, photoBytes, isOriginal); AddSizeToSitePhoto(sitePhoto, blobUri.ToString(), altSize); photoIndexTableAdapter.InsertPhotoIndex(blogname, id, date, SanityHelper.SanitizeSourceBlog(photosToDownload.SourceBlog), blobUri.ToString(), urlHelper.Name, urlHelper.Size, altSize.Width, altSize.Height, altSize.Url); isOriginal = false; log.Info("Downloaded photo from: " + altSize.Url); } } } } if (sitePhoto?.Sizes.Length > 0) { sitePhotos.Add(sitePhoto); } } } string modifiedBody = BodyUrlModifier.ModifyUrls(sourceBlog, photosToDownload.Body, photoIndexTableAdapter, sitePhotos, out List <Model.Tumblr.Photo> extractedPhotos); if (extractedPhotos != null) { log.Warning("Trying to modify body in ProcessPhotosToDownload but some images were not possible to replace"); } postsTableAdapter.MarkPhotosAsDownloaded(photosToDownload.IndexInfo.BlogName, photosToDownload.IndexInfo.PostId, sitePhotos, modifiedBody); ReversePostEntity reversePost = new ReversePostEntity(photosToDownload.IndexInfo.BlogName, photosToDownload.IndexInfo.PostId, photosToDownload.PostType, photosToDownload.IndexInfo.PostDate, modifiedBody, photosToDownload.Title) { Photos = JsonConvert.SerializeObject(sitePhotos) }; reversePostsTableAdapter.InsertPost(reversePost); } catch (Exception ex) { if (ex is HttpRequestException httpRequestException && httpRequestException.Message.Contains("403") && httpRequestException.Message.Contains("Forbidden")) { log.Warning("HTTP request was forbidden to URL: " + requestUrl); }