public Item UploadFile(string targetPath, File fileInfo, string altText)
        {
            string uri = fileInfo.Url.StartsWith("http") ? fileInfo.Url : "https://gathercontent.s3.amazonaws.com/" + fileInfo.Url;

            string extension = string.Empty;

            if (fileInfo.FileName.Contains("."))
            {
                extension = fileInfo.FileName.Substring(fileInfo.FileName.LastIndexOf('.') + 1);
            }

            var request = (HttpWebRequest)WebRequest.Create(uri);
            var resp    = (HttpWebResponse)request.GetResponse();
            var stream  = resp.GetResponseStream();

            using (var memoryStream = new MemoryStream())
            {
                memoryStream.Seek(0, SeekOrigin.Begin);
                if (stream != null)
                {
                    stream.CopyTo(memoryStream);
                }

                if (memoryStream.Length > 0)
                {
                    altText = ItemUtil.ProposeValidItemName(altText);
                    var media = CreateMedia(targetPath, fileInfo, extension, memoryStream, altText);
                    return(media);
                }
            }

            return(null);
        }
예제 #2
0
        protected virtual Item CreateMedia(string rootPath, File mediaFile, string extension, Stream mediaStream)
        {
            using (new SecurityDisabler())
            {
                var validItemName = ItemUtil.ProposeValidItemName(mediaFile.FileName);

                var filesFolder = GetItemByPath(rootPath);
                if (filesFolder != null)
                {
                    var files = filesFolder.Children;
                    var item  = files.FirstOrDefault(f => f.Name == validItemName &&
                                                     DateUtil.IsoDateToDateTime(f.Fields["__Created"].Value) >=
                                                     mediaFile.UpdatedDate);
                    if (item != null)
                    {
                        return(item);
                    }
                }

                var mediaOptions = new MediaCreatorOptions
                {
                    Database  = ContextDatabase,
                    FileBased = false,
                    IncludeExtensionInItemName = false,
                    KeepExisting = true,
                    Versioned    = false,
                    Destination  = string.Concat(rootPath, "/", validItemName)
                };

                var previewImgItem = MediaManager.Creator.CreateFromStream(mediaStream, validItemName + "." + extension, mediaOptions);
                return(previewImgItem);
            }
        }
예제 #3
0
        public Item UploadFile(string targetPath, File fileInfo)
        {
            var gcsettings  = new AccountsRepository().GetAccountSettings();
            var itemService = new ItemsService(gcsettings);

            string extension = string.Empty;

            if (fileInfo.FileName.Contains("."))
            {
                extension = fileInfo.FileName.Substring(fileInfo.FileName.LastIndexOf('.') + 1);
            }

            var memoryStream = itemService.DownloadFile(fileInfo.FileId) as MemoryStream;

            try
            {
                if (memoryStream.Length > 0)
                {
                    var media = CreateMedia(targetPath, fileInfo, extension, memoryStream);
                    return(media);
                }
            }
            finally
            {
                memoryStream.Close();
            }

            return(null);
        }
예제 #4
0
        protected virtual int CreateMedia(string rootPath, File mediaFile, string extension, Stream mediaStream)
        {
            if (mediaStream.Length > 0)
            {
                rootPath += "/" + mediaFile.FileName;
                string[] path         = rootPath.Split('/').Where(x => !string.IsNullOrWhiteSpace(x)).ToArray();
                IMedia   parentFolder = null;

                IMedia itemMedia = null;
                for (int i = 0; i < path.Length; i++)
                {
                    var newItemMedia =
                        ContextService.MediaService.GetChildren(itemMedia?.Id ?? -1).FirstOrDefault(x => x.Name == path[i]);
                    if (i == path.Length - 2)
                    {
                        parentFolder = newItemMedia;
                    }
                    if (newItemMedia == null)
                    {
                        string mediaType = (i < path.Length)
                                                        ? Constants.Conventions.MediaTypes.Folder
                                                        : Constants.Conventions.MediaTypes.File;

                        if (i == path.Length - 1 && IsImage(extension))
                        {
                            mediaType = Constants.Conventions.MediaTypes.Image;
                        }

                        itemMedia = itemMedia != null?ContextService.MediaService.CreateMedia(path[i], itemMedia, mediaType)
                                        : ContextService.MediaService.CreateMedia(path[i], -1, mediaType);

                        ContextService.MediaService.Save(itemMedia);
                    }
                    else
                    {
                        itemMedia = newItemMedia;
                    }
                }
                if (itemMedia != null)
                {
                    try
                    {
                        itemMedia.SetValue(Constants.Conventions.Media.File, mediaFile.FileName, mediaStream);
                    }
                    catch (Exception)
                    {
                        ContextService.MediaService.Delete(itemMedia);
                        var type = IsImage(extension) ? Constants.Conventions.MediaTypes.Image : Constants.Conventions.MediaTypes.File;
                        itemMedia = ContextService.MediaService.CreateMedia(mediaFile.FileName, parentFolder?.Id ?? -1, type);
                        itemMedia.SetValue(Constants.Conventions.Media.File, mediaFile.FileName, mediaStream);
                    }
                    ContextService.MediaService.Save(itemMedia);
                    return(itemMedia.Id);
                }
            }
            return(0);
        }
예제 #5
0
        private int UploadFile(File file, string mediaType, string itemName, string fieldName)
        {
            var uri     = file.Url.StartsWith("http") ? file.Url : "https://gathercontent.s3.amazonaws.com/" + file.Url;
            var request = (HttpWebRequest)WebRequest.Create(uri);
            var resp    = (HttpWebResponse)request.GetResponse();
            var stream  = resp.GetResponseStream();

            using (var memoryStream = new MemoryStream())
            {
                memoryStream.Seek(0, SeekOrigin.Begin);
                stream?.CopyTo(memoryStream);

                if (memoryStream.Length > 0)
                {
                    var gatherContent = ContextService.MediaService.GetChildren(-1).FirstOrDefault(x => x.Name == "GatherContent");
                    if (gatherContent == null)
                    {
                        gatherContent = ContextService.MediaService.CreateMedia("GatherContent", -1, Constants.Conventions.MediaTypes.Folder);
                        ContextService.MediaService.Save(gatherContent);
                    }
                    var itemMedia =
                        ContextService.MediaService.GetChildren(gatherContent.Id).FirstOrDefault(x => x.Name == itemName);
                    if (itemMedia == null)
                    {
                        itemMedia = ContextService.MediaService.CreateMedia(itemName, gatherContent, Constants.Conventions.MediaTypes.Folder);
                        ContextService.MediaService.Save(itemMedia);
                    }
                    var fielfMedia =
                        ContextService.MediaService.GetChildren(itemMedia.Id).FirstOrDefault(x => x.Name == fieldName);
                    if (fielfMedia == null)
                    {
                        fielfMedia = ContextService.MediaService.CreateMedia(fieldName, itemMedia, Constants.Conventions.MediaTypes.Folder);
                        ContextService.MediaService.Save(fielfMedia);
                    }
                    var umbFile = ContextService.MediaService.GetChildren(fielfMedia.Id).FirstOrDefault(x => x.Name == file.FileName) ??
                                  ContextService.MediaService.CreateMedia(file.FileName, fielfMedia, mediaType);
                    umbFile.SetValue(Constants.Conventions.Media.File, file.FileName, memoryStream);
                    ContextService.MediaService.Save(umbFile);
                    return(umbFile.Id);
                }

                return(0);
            }
        }
        protected virtual Item CreateMedia(string rootPath, File mediaFile, string extension, Stream mediaStream, string altText)
        {
            using (new SecurityDisabler())
            {
                var validItemName = ItemUtil.ProposeValidItemName(mediaFile.FileName);

                var filesFolder = GetItemByPath(rootPath);
                if (filesFolder != null)
                {
                    var files = filesFolder.Children;
                    var item  = files.FirstOrDefault(f => f.Name == validItemName &&
                                                     DateUtil.IsoDateToDateTime(f.Fields["__Created"].Value) >=
                                                     mediaFile.UpdatedDate);
                    if (item != null)
                    {
                        return(item);
                    }
                }

                var mediaOptions = new MediaCreatorOptions
                {
                    Database  = ContextDatabase,
                    FileBased = false,
                    IncludeExtensionInItemName = false,
                    KeepExisting = true,
                    Versioned    = false,
                    Destination  = string.Concat(rootPath, "/", validItemName),
                };

                var previewImgItem = MediaManager.Creator.CreateFromStream(mediaStream, validItemName + "." + extension, mediaOptions);
                //mediaOptions.AlternateText -- This is not working on the PROD.
                //So forciing ALT Update using below code --- This works on the PROD.
                //Update ALT Text
                previewImgItem.Editing.BeginEdit();
                previewImgItem["Alt"] = altText;
                previewImgItem.Editing.EndEdit();
                return(previewImgItem);
            }
        }