示例#1
0
        public void TestSimple()
        {
            //var binaryContentProvider = Providers.GetProviderFactory(repository.DBProvider).GetProvider<IContentProvider<BinaryContent>>();

            var binaryContent1 = new MediaContent(repository.Name, binaryFolder.Name);
            binaryContent1.UserKey = "image1";
            binaryContent1.VirtualPath = "~/binary/image1.jpg";
            binaryContent1.ContentFile = new ContentFile()
            {
                Name = "image1.jpg",
                FileName = "image1.jpg",
                Stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5, 6 })
            };
            binaryContentProvider.Add(binaryContent1);

            var binaryContent2 = new MediaContent(repository.Name, binaryFolder.Name);
            binaryContent2.UserKey = "image2";
            binaryContent2.VirtualPath = "~/binary/image2.jpg";
            binaryContent2.ContentFile = new ContentFile()
            {
                Name = "image2.jpg",
                FileName = "image2.jpg",
                Stream = new MemoryStream(new byte[] { 1, 2, 3, 4, 5, 6 })
            };
            binaryContentProvider.Add(binaryContent2);

            var query1 = binaryFolder.CreateQuery().WhereEquals("UUID", (string)(binaryContent1.UUID));
            Assert.AreEqual(1, query1.Count());
            var binaryContentItem1 = query1.First();
            Assert.AreEqual(binaryContent1.UUID, binaryContentItem1.UUID);
        }
示例#2
0
        public static MediaContent BlobToMediaContent(this CloudBlob blob, MediaContent mediaContent)
        {
            if (!string.IsNullOrEmpty(blob.Metadata["Published"]))
            {
                mediaContent.Published = bool.Parse(blob.Metadata["Published"]);
            }
            if (!string.IsNullOrEmpty(blob.Metadata["Size"]))
            {
                mediaContent.Size = int.Parse(blob.Metadata["Size"]);
            }
            mediaContent.FileName = blob.Metadata["FileName"];
            mediaContent.UserKey = mediaContent.FileName;
            mediaContent.UUID = mediaContent.FileName;
            mediaContent.UserId = blob.Metadata["UserId"];
            mediaContent.VirtualPath = blob.Uri.ToString();
            if (mediaContent.Metadata == null)
            {
                mediaContent.Metadata = new MediaContentMetadata();
            }

            mediaContent.Metadata.AlternateText = blob.Metadata["AlternateText"];
            mediaContent.Metadata.Description = blob.Metadata["Description"];
            mediaContent.Metadata.Title = blob.Metadata["Title"];
            return mediaContent;
        }
示例#3
0
        public virtual MediaContent Update(Repository repository, MediaFolder mediaFolder, string uuid, string fileName, Stream fileStream,
            string userid, MediaContentMetadata metadata)
        {
            IsAllowExtension(fileName, mediaFolder.AllowedExtensions);


            var binaryContent = mediaFolder.CreateQuery().WhereEquals("UUID", uuid).First();
            var old = new MediaContent(binaryContent);
            binaryContent.UserId = userid;
            binaryContent.UtcLastModificationDate = DateTime.UtcNow;

            binaryContent.FileName = fileName;
            binaryContent.UserKey = fileName;
            binaryContent.UUID = fileName;

            if (fileStream != null)
            {
                binaryContent.ContentFile = new ContentFile()
                {
                    Name = fileName,
                    FileName = fileName,
                    Stream = fileStream
                };
                Provider.SaveContentStream(old, fileStream);
            }

            binaryContent.Metadata = metadata;
            Provider.Update(binaryContent, old);

            return binaryContent;
        }
示例#4
0
        public void Test1()
        {
            MediaContent binaryContent1 = new MediaContent();
            binaryContent1.Repository = repository.Name;
            binaryContent1.FolderName = binaryFolder.Name;
            binaryContent1.FileName = "file1.jpg";
            binaryContent1.VirtualPath = "~/file1.jpg";
            Providers.DefaultProviderFactory.GetProvider<IMediaContentProvider>().Add(binaryContent1);

            MediaContent binaryContent2 = new MediaContent();
            binaryContent2.Repository = repository.Name;
            binaryContent2.FolderName = binaryFolder.Name;
            binaryContent2.FileName = "file2.jpg";
            binaryContent2.VirtualPath = "~/file2.jpg";
            Providers.DefaultProviderFactory.GetProvider<IMediaContentProvider>().Add(binaryContent2);

            var query = binaryFolder.CreateQuery().OrderBy("id");

            Assert.AreEqual(2, query.Count());

            Assert.AreEqual("file1.jpg", query.First().FileName);
            Assert.AreEqual("file2.jpg", query.Last().FileName);

            Providers.DefaultProviderFactory.GetProvider<IMediaContentProvider>().Delete(binaryContent1);
            Assert.AreEqual(1, query.Count());
        }
示例#5
0
        public virtual MediaContent Add(Repository repository, MediaFolder mediaFolder, string fileName, Stream fileStream, bool @overrided,
            string userId, MediaContentMetadata metadata)
        {
            fileName = UrlUtility.ToUrlString(Path.GetFileNameWithoutExtension(fileName)) + Path.GetExtension(fileName);
            IsAllowExtension(fileName, mediaFolder.AllowedExtensions);

            var mediaContent = new MediaContent(repository.Name, mediaFolder.FullName);

            mediaContent.UserId = userId;
            mediaContent.Published = true;

            mediaContent.FileName = fileName;

            mediaContent.UserKey = fileName;
            mediaContent.UUID = fileName;

            mediaContent.ContentFile = new ContentFile()
            {
                Name = fileName,
                FileName = fileName,
                Stream = fileStream
            };

            mediaContent.UtcLastModificationDate = mediaContent.UtcCreationDate = DateTime.UtcNow;
            mediaContent.Metadata = metadata;
            Provider.Add(mediaContent, @overrided);

            return mediaContent;
        }
示例#6
0
 public void SaveMetadata(MediaContent mediaContent)
 {
     if (mediaContent.Metadata != null)
     {
         string metadataFile = GetMetadataFilePath(mediaContent);
         Kooboo.Runtime.Serialization.DataContractSerializationHelper.Serialize(mediaContent.Metadata, metadataFile);
     }
 }
示例#7
0
        public void Delete(MediaContent content)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var contentBlob = blobClient.GetBlobReference(content.GetMediaBlobPath());

            contentBlob.DeleteIfExists();
        }
示例#8
0
 public void DeleteMetadata(MediaContent mediaContent)
 {
     string metadataFile = GetMetadataFilePath(mediaContent);
     if (File.Exists(metadataFile))
     {
         File.Delete(metadataFile);
     }
 }
示例#9
0
 public virtual void MoveMetadata(MediaContent old, MediaContent @new)
 {
     var oldPath = GetMetadataFilePath(old);
     if (File.Exists(oldPath))
     {
         var newPath = GetMetadataFilePath(@new);
         File.Move(oldPath, newPath);
     }
 }
示例#10
0
 public void Test_Add_Content()
 {
     MediaContent content = new MediaContent(repository.Name, mediaFolder.FullName);
     content.Published = true;
     content.ContentFile = new ContentFile()
     {
         FileName = "Test_Add_Content.dat",
         Stream = new MemoryStream(new byte[10])
     };
     provider.Add(content);
     Console.WriteLine(content.VirtualPath);
 }
示例#11
0
 public void FillMetadata(MediaContent mediaContent)
 {
     string metadataFile = GetMetadataFilePath(mediaContent);
     if (File.Exists(metadataFile))
     {
         var metadata = Kooboo.Runtime.Serialization.DataContractSerializationHelper.Deserialize<MediaContentMetadata>(metadataFile);
         mediaContent.Metadata = metadata;
         mediaContent["Metadata.Title"] = metadata.Title;
         mediaContent["Metadata.AlternateText"] = metadata.AlternateText;
         mediaContent["Metadata.Description"] = metadata.Description;
     }
 }
示例#12
0
        public void Test_Delete_Content()
        {
            MediaContent content = new MediaContent(repository.Name, mediaFolder.FullName);
            content.Published = true;
            content.ContentFile = new ContentFile()
            {
                FileName = "Test_Delete_Content.zip",
                Stream = new MemoryStream(new byte[10])
            };
            provider.Add(content);

            provider.Delete(content);
        }
示例#13
0
        public void Add(MediaContent content, bool overrided)
        {
            if (content.ContentFile != null)
            {
                content.FileName = content.ContentFile.FileName;

                content.UserKey = content.FileName;
                content.UUID = content.FileName;

                RemoteServiceFactory.CreateService<IMediaContentService>().Add(
                    new MediaContentParameter()
                    {
                        MediaContent = content,
                        FileData = content.ContentFile.Stream.ReadData()
                    });
            }
        }
示例#14
0
 public static MediaContent BlobToMediaContent(this CloudBlob blob, MediaContent mediaContent)
 {
     if (!string.IsNullOrEmpty(blob.Metadata["Published"]))
     {
         mediaContent.Published = bool.Parse(blob.Metadata["Published"]);
     }
     if (!string.IsNullOrEmpty(blob.Metadata["Size"]))
     {
         mediaContent.Size = int.Parse(blob.Metadata["Size"]);
     }
     mediaContent.FileName = blob.Metadata["FileName"];
     mediaContent.UserKey = mediaContent.FileName;
     mediaContent.UUID = mediaContent.FileName;
     mediaContent.UserId = blob.Metadata["UserId"];
     mediaContent.VirtualPath = blob.Uri.ToString();
     return mediaContent;
 }
示例#15
0
        public void Test_Get_Content()
        {
            MediaContent content = new MediaContent(repository.Name, mediaFolder.FullName);
            content.Published = true;
            content.UserId = "admin";
            content.ContentFile = new ContentFile()
            {
                FileName = "Test_Get_Content.zip",
                Stream = new MemoryStream(new byte[10])
            };
            provider.Add(content);

            var gotContent = mediaFolder.CreateQuery().WhereEquals("FileName", content.FileName).First();
            Assert.AreEqual(content.FileName, gotContent.FileName);
            Assert.AreEqual(content.UserId, gotContent.UserId);
            Assert.AreEqual(content.Published, gotContent.Published);
            Console.WriteLine(content.VirtualPath, gotContent.VirtualPath);
        }
示例#16
0
        public void Add(MediaContent content, bool overrided)
        {
            if (content.ContentFile != null)
            {
                content.FileName = content.ContentFile.FileName;

                content.UserKey = content.FileName;
                content.UUID = content.FileName;

                var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

                var contentBlob = blobClient.GetBlobReference(content.GetMediaBlobPath());
                contentBlob = content.MediaContentToBlob(contentBlob);

                contentBlob.UploadFromStream(content.ContentFile.Stream);

                content.VirtualPath = contentBlob.Uri.ToString();
            }
        }
示例#17
0
        private static void MoveContent(MediaContent oldMediaContent, MediaContent newMediaContent)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var oldContentBlob = blobClient.GetBlobReference(oldMediaContent.GetMediaBlobPath());
            var newContentBlob = blobClient.GetBlobReference(newMediaContent.GetMediaBlobPath());
            newContentBlob.CopyFromBlob(oldContentBlob);

            oldContentBlob.DeleteIfExists();
        }
示例#18
0
        public void Update(MediaContent @new, MediaContent old)
        {
            if ([email protected](old.FileName, StringComparison.OrdinalIgnoreCase))
            {
                MoveContent(old, @new);
            }
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var contentBlob = blobClient.GetBlobReference(@new.GetMediaBlobPath());

            contentBlob = @new.MediaContentToBlob(contentBlob);

            contentBlob.SetMetadata();

            @new.VirtualPath = contentBlob.Uri.ToString();
        }
示例#19
0
        public void Move(MediaFolder sourceFolder, string oldFileName, MediaFolder targetFolder, string newFileName)
        {
            var oldMediaContent = new MediaContent() { Repository = sourceFolder.Repository.Name, FolderName = sourceFolder.FullName, UUID = oldFileName, FileName = oldFileName };
            var newMediaContent = new MediaContent() { Repository = targetFolder.Repository.Name, FolderName = targetFolder.FullName, UUID = newFileName, FileName = newFileName };

            MoveContent(oldMediaContent, newMediaContent);
        }
示例#20
0
        public void SaveContentStream(MediaContent content, Stream stream)
        {
            if (stream.Length == 0)
            {
                return;
            }
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();
            var contentBlob = blobClient.GetBlobReference(content.GetMediaBlobPath());

            if (contentBlob.Exists())
            {
                contentBlob.UploadFromStream(stream);
            }
        }
示例#21
0
        public void Add(MediaContent content, bool overrided)
        {
            if (content.ContentFile != null)
            {

                content.FileName = content.ContentFile.FileName;

                content.UserKey = content.FileName;
                content.UUID = content.FileName;

                // if the file already exist and dont need to overrided just return
                if (content.Exist() && !overrided)
                {
                    return;
                }

                var contentPath = new MediaContentPath(content);

                content.VirtualPath = contentPath.VirtualPath;

                locker.EnterWriteLock();

                try
                {
                    content.ContentFile.Stream.SaveAs(contentPath.PhysicalPath);
                }
                finally
                {
                    locker.ExitWriteLock();
                }

                SetFilePublished(contentPath.PhysicalPath, content.Published);

                metadataStorage.SaveMetadata(content);
            }
        }
示例#22
0
 public string GetMetadataFilePath(MediaContent mediaContent)
 {
     return new MediaContentPath(mediaContent).PhysicalPath + MetadataFileExtension;
 }
示例#23
0
        /// <summary>
        /// Exists the specified content.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns></returns>
        public static bool Exist(this MediaContent content)
        {
            var contentPath = new MediaContentPath(content);

            return(File.Exists(contentPath.PhysicalPath));
        }
示例#24
0
 /// <summary>
 /// Gets the folder.
 /// </summary>
 /// <param name="content">The content.</param>
 /// <returns></returns>
 public static MediaFolder GetFolder(this MediaContent content)
 {
     return(new MediaFolder(content.GetRepository(), FolderHelper.SplitFullName(content.FolderName)));
 }
示例#25
0
 public void Delete(MediaContent content)
 {
     RemoteServiceFactory.CreateService<IMediaContentService>().Delete(content.Repository, content.FolderName, content.UUID);
 }
示例#26
0
 public void MoveMetadata(MediaContent old, MediaContent @new)
 {
     var oldPath = GetMetadataFilePath(old);
     var newPath = GetMetadataFilePath(@new);
     File.Move(oldPath, newPath);
 }
示例#27
0
        /// <summary>
        /// Resizes the image.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="preserverAspectRatio">The preserver aspect ratio.保持比例</param>
        /// <param name="quality">The quality.</param>
        /// <returns></returns>
        public virtual ActionResult ResizeImage(string url, int width, int height, bool? preserverAspectRatio, int? quality)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException(url);
            }
            url = HttpUtility.UrlDecode(url);
            var index = url.IndexOf("?");
            if (index != -1)
            {
                url = url.Substring(0, index);
            }

            preserverAspectRatio = preserverAspectRatio ?? true;
            quality = quality ?? 80;

            if (url.StartsWith("http://") || url.StartsWith("https://"))
            {
                //now no image cache for azure blob
                var provider = Kooboo.CMS.Content.Persistence.Providers.DefaultProviderFactory.GetProvider<Kooboo.CMS.Content.Persistence.IMediaContentProvider>();
                var mediaContent = new MediaContent() { VirtualPath = url };
                var data = provider.GetContentStream(mediaContent);
                if (data != null)
                {
                    using (var imageStream = new MemoryStream(data))
                    {
                        var imageFormat = ImageTools.ConvertToImageFormat(Path.GetExtension(mediaContent.VirtualPath));
                        Stream outStream = new MemoryStream();
                        ImageTools.ResizeImage(imageStream, outStream, imageFormat, width, height, preserverAspectRatio.Value, quality.Value);
                        outStream.Position = 0;
                        return File(outStream, IOUtility.MimeType(url));
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                var imageFullPath = Server.MapPath(url);
                var cachingPath = GetCachingFilePath(imageFullPath, width, height, preserverAspectRatio.Value, quality.Value);

                if (!System.IO.File.Exists(cachingPath))
                {
                    if (!System.IO.File.Exists(cachingPath))
                    {
                        var dir = Path.GetDirectoryName(cachingPath);
                        IOUtility.EnsureDirectoryExists(dir);
                        var success = ImageTools.ResizeImage(imageFullPath, cachingPath, width, height, preserverAspectRatio.Value, quality.Value);
                        if (!success)
                        {
                            cachingPath = imageFullPath;
                        }
                    }
                }
                SetCache(HttpContext.Response, 2592000, "*");
                return File(cachingPath, IOUtility.MimeType(imageFullPath));
            }
        }
示例#28
0
        private static void MoveContent(MediaContent oldMediaContent, MediaContent newMediaContent)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();

            var oldContentBlob = blobClient.GetBlockBlobReference(oldMediaContent.GetMediaBlobPath());
            var newContentBlob = blobClient.GetBlockBlobReference(newMediaContent.GetMediaBlobPath());
            if (oldContentBlob.Exists() && !newContentBlob.Exists())
            {
                try
                {
                    newContentBlob.CopyFromBlob(oldContentBlob);
                }
                catch (Exception e)
                {
                    using (Stream stream = new MemoryStream())
                    {
                        oldContentBlob.DownloadToStream(stream);
                        stream.Position = 0;
                        newContentBlob.UploadFromStream(stream);
                        stream.Dispose();
                    }
                }
                newContentBlob.Metadata["FileName"] = StorageNamesEncoder.EncodeBlobName(newMediaContent.FileName);
                if (!string.IsNullOrEmpty(oldContentBlob.Metadata["UserId"]))
                {
                    newContentBlob.Metadata["UserId"] = oldContentBlob.Metadata["UserId"];
                }
                if (!string.IsNullOrEmpty(oldContentBlob.Metadata["Published"]))
                {
                    newContentBlob.Metadata["Published"] = oldContentBlob.Metadata["Published"];
                }
                if (!string.IsNullOrEmpty(oldContentBlob.Metadata["Size"]))
                {
                    newContentBlob.Metadata["Size"] = oldContentBlob.Metadata["Size"];
                }
                if (oldContentBlob.Metadata.AllKeys.Contains("AlternateText"))
                {
                    newContentBlob.Metadata["AlternateText"] = oldContentBlob.Metadata["AlternateText"];
                }
                if (oldContentBlob.Metadata.AllKeys.Contains("Description"))
                {
                    newContentBlob.Metadata["Description"] = oldContentBlob.Metadata["Description"];
                }
                if (oldContentBlob.Metadata.AllKeys.Contains("Title"))
                {
                    newContentBlob.Metadata["Title"] = oldContentBlob.Metadata["Title"];
                }

                newContentBlob.SetMetadata();
                oldContentBlob.DeleteIfExists();
            }

        }
示例#29
0
 public void Add(MediaContent content)
 {
     Add(content, true);
 }
示例#30
0
        public byte[] GetContentStream(MediaContent content)
        {
            var blobClient = CloudStorageAccountHelper.GetStorageAccount().CreateCloudBlobClient();
            var path = string.Empty;
            if (string.IsNullOrEmpty(content.GetRepository().Name))
            {
                path = content.VirtualPath.Substring(AzureBlobServiceSettings.Instance.Endpoint.Length);
                //path = UrlUtility.Combine(path.Split('/').Select(it => StorageNamesEncoder.EncodeBlobName(it)).ToArray());
            }
            else
            {
                path = content.GetMediaBlobPath();
            }
            var contentBlob = blobClient.GetBlobReference(path);

            byte[] data = null;
            if (contentBlob.Exists())
            {
                data = contentBlob.DownloadByteArray();
            }
            return data;
        }
示例#31
0
 public void Update(MediaContent @new, MediaContent old)
 {
     if ([email protected](old.FileName, StringComparison.OrdinalIgnoreCase))
     {
         RemoteServiceFactory.CreateService<IMediaContentService>().Move(old.Repository,
             old.FolderName, old.FileName, @new.FolderName, @new.FileName);
     }
     else
     {
         if (@new.ContentFile != null)
         {
             RemoteServiceFactory.CreateService<IMediaContentService>().Update(new MediaContentParameter()
             {
                 MediaContent = @new,
                 FileData = @new.ContentFile.Stream.ReadData()
             });
         }
     }
 }
示例#32
0
        public virtual void Update(MediaFolder folder, string uuid, IEnumerable<string> fieldNames, IEnumerable<object> fieldValues)
        {
            var mediaFolder = (MediaFolder)folder;
            var content = mediaFolder.CreateQuery().WhereEquals("uuid", uuid).FirstOrDefault();

            if (content != null)
            {
                var newContent = new MediaContent(content);
                var names = fieldNames.ToArray();
                var values = fieldValues.ToArray();
                for (int i = 0; i < names.Length; i++)
                {
                    newContent[names[i]] = values[i];
                }

                Provider.Update(newContent, content);
            }

        }