Exemplo n.º 1
0
        public void FileContentIsntStoredIfTransactionWasRolledBack()
        {
            string storagePath = string.Empty;

            using (var unitOfWork = new UnitOfWork())
            {
                var folder = new Folder
                {
                    Name = "Test folder",
                };
                Repository.Data.Save(folder);

                var file = new File
                {
                    //Folder = folder,
                    DisplayName = "Test file",
                    Name        = "TestFile.txt",
                    ContentType = FileType.txt
                };
                Stream stream = new MemoryStream();
                byte[] buffer = Guid.NewGuid().ToByteArray();
                stream.Write(buffer, 0, buffer.Length);
                file.FileStream = new Lazy <Stream>(() => stream);
                Repository.Data.Save(file);
                storagePath = file.StoragePath;

                unitOfWork.Rollback();
            }

            string path = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, storagePath);

            Assert.False(System.IO.File.Exists(path));
        }
Exemplo n.º 2
0
        public void FileContentIsntDeletedIfTransactionWasRolledBack()
        {
            var folder = new Folder
            {
                Name = "Test folder"
            };
            Repository.Data.Save(folder);

            var file = new File
            {
                //Folder = folder,
                DisplayName = "Test file",
                Name = "TestFile.txt",
                ContentType = FileType.txt
            };
            Stream stream = new MemoryStream();
            byte[] buffer = Guid.NewGuid().ToByteArray();
            stream.Write(buffer, 0, buffer.Length);
            file.FileStream = new Lazy<Stream>(() => stream);
            Repository.Data.Save(file);

            using (var unitOfWork = new UnitOfWork())
            {
                Repository.Data.Delete(file);

                unitOfWork.Rollback();
            }

            string path = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, file.StoragePath);
            Assert.True(System.IO.File.Exists(path));

            Repository.Data.Delete(file);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a copy of the file in a new parent folder.
        /// </summary>
        /// <param name="newParentFolder">New parent folder.</param>
        /// <param name="file">Copied file.</param>
        /// <returns>The file copy, without file value.</returns>
        public File Copy(Folder newParentFolder, File file)
        {
            if (newParentFolder == null)
                throw new NullReferenceException("The new ParentFolder can't be null");

            return Copy(newParentFolder.Id, file);
        }
Exemplo n.º 4
0
        public string GetImageUrl(Guid productEntityId, EntityType type)
        {
            string imageUrl = String.Empty;
            LFile  image    = null;

            switch (type)
            {
            case EntityType.Product:
            {
                image =
                    Repository.Data.Get <LFile>().Where(
                        x => x.EntityId == productEntityId && x.ResizedTo == ResizedVersion.To100x125).FirstOrDefault().
                    Value;
                break;
            }

            default:
            {
                image =
                    Repository.Data.Get <LFile>().Where(
                        x => x.EntityId == productEntityId && x.ResizedTo == ResizedVersion.To620x195).FirstOrDefault().
                    Value;
                break;
            }
            }

            if (image != null)
            {
                imageUrl = String.Format("/{0}/{1}", LitiumConfigs.Data.FilesStorage.Replace("\\", "/"), image.Name);
            }

            return(imageUrl);
        }
        public void CopyFileEnterParameterFolderIdTest()
        {
            var folder1 = new Folder { Name = "Folder1" };
            Repository.Data.Save(folder1);
            File file1 = new File { ContentType = FileType.txt, Name = "Dummy", DisplayName = "Dummy"};
            Repository.Data.Save(file1);
            File copyFile1 = _service.Copy(folder1.Id, file1);

            //Assert.Equal(folder1, copyFile1.Folder);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Move file from a folder to another folder.
        /// </summary>
        /// <param name="newParentFolder">New folder.</param>
        /// <param name="file">The file which will be moved.</param>
        public void Move(Folder newParentFolder, File file)
        {
            if (newParentFolder == null)
                throw new NullReferenceException("The new ParentFolder can't be null");

            if (newParentFolder.Id == Guid.Empty)
                throw new ArgumentOutOfRangeException("newParentFolderId", "Value can't be Guid.Empty");

            if (file == null)
                throw new NullReferenceException("File can't be null");

            //Repository.Data.Save(file.Folder = newParentFolder);
        }
        public void ValidationFileInsertTest()
        {
            var folder = new Folder {Name = "Folder1"};
            Repository.Data.Save(folder);
            Assert.NotNull(folder);

            var file = new File
                       	{
                            ContentType = FileType.image,
                       		DisplayName = string.Empty,
                       		Name = string.Empty,
                       	};

            Assert.Throws<ValidationArgumentException>(() => Repository.Data.Save(file));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates a copy of the file in a new parent folder.
        /// </summary>
        /// <param name="newParentFolderId">Parent folder Id.</param>
        /// <param name="file">File what will be copied into new folder.</param>
        /// <returns>The file copy, without file value.</returns>
        public File Copy(Guid newParentFolderId, File file)
        {
            if (newParentFolderId == Guid.Empty)
                throw new ArgumentOutOfRangeException("newParentFolderId", "Value can't be Guid.Empty");

            if (file == null)
                throw new NullReferenceException("File can't be null");

            var folder = Repository.Data.Get<Folder>(newParentFolderId);

            if (folder == null)
                throw new ArgumentNullException("The parent folder doesn't exist.");

            File newFile = (File)file.Clone();
            //newFile.Folder = folder;
            Repository.Data.Save(newFile);
            return newFile;
        }
Exemplo n.º 9
0
        public void ValidationFileInsertTest()
        {
            var folder = new Folder {
                Name = "Folder1"
            };

            Repository.Data.Save(folder);
            Assert.NotNull(folder);

            var file = new File
            {
                ContentType = FileType.image,
                DisplayName = string.Empty,
                Name        = string.Empty,
            };

            Assert.Throws <ValidationArgumentException>(() => Repository.Data.Save(file));
        }
Exemplo n.º 10
0
        public string UploadProductImageAsync <T>(IEnumerable <HttpPostedFileBase> attachments, string entityInput, ResizedVersion resizedVersion) where T : IImage
        {
            Guid entityId;

            if (Guid.TryParse(entityInput, out entityId))
            {
                foreach (var file in attachments)
                {
                    var fileName = Path.GetFileName(file.FileName);

                    //if (CheckIfImageForProductExist (Repository.Data.Get<T> (entityId).Image, fileName))
                    //return String.Empty;

                    FileType fileType;
                    Enum.TryParse(file.ContentType.Replace("image/", String.Empty), out fileType);

                    if (fileType != FileType.other && fileType < FileType.gif)
                    {
                        HttpPostedFileBase file1 = file;
                        LFile image          = CreateImageEntity(fileName, file1);
                        LFile scallableImage = CreateScallableImageEnity(file1, image, resizedVersion);

                        //Repository.Data.Save (image);
                        scallableImage.EntityId = entityId;
                        Repository.Data.Save(scallableImage);

                        var entity = Repository.Data.Get <T>(entityId);
                        entity.HasImage = true;

                        //entity.Image.Add (image);
                        Repository.Data.Save(entity);
                    }
                    else
                    {
                        return(StoreResourceStrings.OnlyImagesIsAllovedToUpload);
                    }
                }
            }
            return(String.Empty);
        }
Exemplo n.º 11
0
        private LFile CreateImageEntity(string fileName, HttpPostedFileBase file1)
        {
            var image = new Litium.Domain.Entities.Media.File();

            image.Name = fileName;

            Stream copiedFileStream = new MemoryStream();

            file1.InputStream.CopyTo(copiedFileStream);

            image.FileStream  = new Lazy <Stream> (() => copiedFileStream);
            image.DisplayName = file1.FileName;

            image.StoragePath = HttpContext
                                .Current.Request.MapPath(LitiumConfigs.Data.FilesStorage)
                                .Replace(@"Shared\", String.Empty);

            image.ContentType = FileType.image;
            image.Size        = file1.ContentLength;
            image.ResizedTo   = ResizedVersion.Origin;
            return(image);
        }
Exemplo n.º 12
0
        private LFile CreateScallableImageEnity(HttpPostedFileBase file1, LFile image, ResizedVersion resizedVersion)
        {
            var scallableImage = (LFile)image.Clone();

            int width  = 100;
            int height = 125;

            if (resizedVersion == ResizedVersion.To100x125)
            {
                width = 100; height = 125;
            }
            if (resizedVersion == ResizedVersion.To620x195)
            {
                width = 620; height = 195;
            }

            Stream imageStream = ScalableImageUtility.GetResizedVersionStream(width, height, file1.InputStream, ImageFormat.Jpeg, true);

            scallableImage.FileStream = new Lazy <Stream> (() => imageStream);
            scallableImage.ResizedTo  = resizedVersion;
            scallableImage.Name       = String.Format("Resized{0}_{1}", resizedVersion, image.Name);
            return(scallableImage);
        }
Exemplo n.º 13
0
        public void FileContentIsStoredLoadedDeletedTogetherWithFileEntity()
        {
            //Create new file with content, save it and check if file was stored on the disk.
            var folder = new Folder
            {
                Name = "Test folder",
            };

            Repository.Data.Save(folder);

            var file = new File
            {
                //Folder = folder,
                DisplayName = "Test file",
                Name        = "TestFile.txt",
                ContentType = FileType.txt
            };
            Stream stream = new MemoryStream();

            byte[] buffer = Guid.NewGuid().ToByteArray();
            stream.Write(buffer, 0, buffer.Length);
            file.FileStream = new Lazy <Stream>(() => stream);
            Repository.Data.Save(file);
            Assert.NotEmpty(file.StoragePath);
            Assert.Equal(file.Size, buffer.Length);
            string path     = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, file.StoragePath);
            var    fileInfo = new FileInfo(path);

            Assert.True(fileInfo.Exists);
            Assert.Equal(fileInfo.Length, buffer.Length);

            //Get file and check if file content is initialized.
            Repository.Data.Cache.Clear(file);
            var retrievedFile = Repository.Data.Get <File>(file.Id);

            Assert.False(string.IsNullOrEmpty(retrievedFile.StoragePath));
            Assert.Equal(retrievedFile.Size, buffer.Length);
            Assert.NotEqual(retrievedFile.FileStream.Value, null);
            Assert.Equal(retrievedFile.FileStream.Value.Length, buffer.Length);

            //Initialize file with the new file content, save it and check if old file content was deleted from the disk and new one was stored.
            byte[] newBuffer = Guid.NewGuid().ToByteArray().Take(5).ToArray();
            Stream newStream = new MemoryStream();

            newStream.Write(newBuffer, 0, newBuffer.Length);
            retrievedFile.FileStream = new Lazy <Stream>(() => newStream);
            retrievedFile.Name       = "NewTestFile.txt";
            Repository.Data.Save(retrievedFile);
            Assert.NotEmpty(retrievedFile.StoragePath);
            Assert.Equal(retrievedFile.Size, newBuffer.Length);
            string newPath     = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, retrievedFile.StoragePath);
            var    newFileInfo = new FileInfo(newPath);

            Assert.True(newFileInfo.Exists);
            Assert.Equal(newFileInfo.Length, newBuffer.Length);
            Assert.False(System.IO.File.Exists(path));

            //Delete file and check if file content was also deleted from the disk.
            Repository.Data.Delete(retrievedFile);
            Repository.Data.Cache.Clear(retrievedFile);
            var deletedFile = Repository.Data.Get <File>(retrievedFile.Id);

            Assert.Null(deletedFile);
            Assert.False(System.IO.File.Exists(newPath));
        }
Exemplo n.º 14
0
 /// <summary>
 /// Create file copy.
 /// </summary>
 /// <param name="file">File which will be cloned.</param>
 /// <returns>The new file.</returns>
 private File CloneFile(File file)
 {
     return new File
     {
         ContentType = file.ContentType,
         DisplayName = file.DisplayName,
         Size = file.Size,
         Name = file.Name,
         //Folder = file.Folder
     };
 }
Exemplo n.º 15
0
        /// <summary>
        ///  Override. Move file from a folder to another folder.
        /// </summary>
        /// <param name="newParentFolderId">New folder Id.</param>
        /// <param name="file">The file.</param>
        public void Move(Guid newParentFolderId, File file)
        {
            if (newParentFolderId == Guid.Empty)
                throw new ArgumentOutOfRangeException("newParentFolderId", "Value can't be Guid.Empty");

            if (file == null)
                throw new NullReferenceException("File can't be null");

            var folder = Repository.Data.Get<Folder>(newParentFolderId);

            if (folder == null)
                throw new ArgumentNullException("The parent folder doesn't exist.");

            //Repository.Data.Save(file.Folder = folder);
        }
Exemplo n.º 16
0
        public void FileContentIsStoredLoadedDeletedTogetherWithFileEntity()
        {
            //Create new file with content, save it and check if file was stored on the disk.
            var folder = new Folder
                         	{
                         		Name = "Test folder",
                         	};
            Repository.Data.Save(folder);

            var file = new File
                       	{
                       		//Folder = folder,
                       		DisplayName = "Test file",
                       		Name = "TestFile.txt",
                            ContentType = FileType.txt
                       	};
            Stream stream = new MemoryStream();
            byte[] buffer = Guid.NewGuid().ToByteArray();
            stream.Write(buffer, 0, buffer.Length);
            file.FileStream = new Lazy<Stream>(() => stream);
            Repository.Data.Save(file);
            Assert.NotEmpty(file.StoragePath);
            Assert.Equal(file.Size, buffer.Length);
            string path = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, file.StoragePath);
            var fileInfo = new FileInfo(path);
            Assert.True(fileInfo.Exists);
            Assert.Equal(fileInfo.Length, buffer.Length);

            //Get file and check if file content is initialized.
            Repository.Data.Cache.Clear(file);
            var retrievedFile = Repository.Data.Get<File>(file.Id);
            Assert.False(string.IsNullOrEmpty(retrievedFile.StoragePath));
            Assert.Equal(retrievedFile.Size, buffer.Length);
            Assert.NotEqual(retrievedFile.FileStream.Value, null);
            Assert.Equal(retrievedFile.FileStream.Value.Length, buffer.Length);

            //Initialize file with the new file content, save it and check if old file content was deleted from the disk and new one was stored.
            byte[] newBuffer = Guid.NewGuid().ToByteArray().Take(5).ToArray();
            Stream newStream = new MemoryStream();
            newStream.Write(newBuffer, 0, newBuffer.Length);
            retrievedFile.FileStream = new Lazy<Stream>(() => newStream);
            retrievedFile.Name = "NewTestFile.txt";
            Repository.Data.Save(retrievedFile);
            Assert.NotEmpty(retrievedFile.StoragePath);
            Assert.Equal(retrievedFile.Size, newBuffer.Length);
            string newPath = string.Format(@"{0}\{1}", ConfiguredStorageDirectory, retrievedFile.StoragePath);
            var newFileInfo = new FileInfo(newPath);
            Assert.True(newFileInfo.Exists);
            Assert.Equal(newFileInfo.Length, newBuffer.Length);
            Assert.False(System.IO.File.Exists(path));

            //Delete file and check if file content was also deleted from the disk.
            Repository.Data.Delete(retrievedFile);
            Repository.Data.Cache.Clear(retrievedFile);
            var deletedFile = Repository.Data.Get<File>(retrievedFile.Id);
            Assert.Null(deletedFile);
            Assert.False(System.IO.File.Exists(newPath));
        }