Exemplo n.º 1
0
        /// <summary>Deletes files and optionally directories in given path.</summary>
        /// <param name="path">Path to delete.</param>
        /// <param name="directories">Indicates, whether directories should be also deleted.</param>
        private void DeleteInternal(string path, bool directories)
        {
            path = PathHelper.GetValidPath(path);
            var keyVersions = this.Provider
                              .GetObjectsList(path, directories ? ObjectTypeEnum.FilesAndDirectories : ObjectTypeEnum.Files, true, true, false)
                              .ConvertAll(p => new KeyVersion()
            {
                Key = PathHelper.GetObjectKeyFromPath(p, true)
            })
                              .Batch(S3ObjectInfoProvider.MAX_OBJECTS_PER_REQUEST);

            foreach (IEnumerable <KeyVersion> source in keyVersions)
            {
                DeleteObjectsRequest request = new DeleteObjectsRequest()
                {
                    BucketName = AccountInfo.Current.BucketName
                };
                request.Objects = source.ToList();
                try
                {
                    AccountInfo.Current.S3Client.DeleteObjects(request);
                }
                catch (DeleteObjectsException)
                {
                    throw new Exception($"Some of the directory '{path}' underlying objects weren't deleted correctly");
                }
            }
            this.Provider.DeleteObject(S3ObjectFactory.GetInfo(CMS.IO.Path.EnsureEndBackslash(path)));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines whether the given path refers to an existing directory on Amazon S3 storage.
        /// </summary>
        /// <param name="path">Path to test.</param>
        public static bool ExistsInS3Storage(string path)
        {
            path = PathHelper.GetValidPath(path);
            string objectKeyFromPath = PathHelper.GetObjectKeyFromPath(path);

            if (!string.IsNullOrEmpty(objectKeyFromPath))
            {
                return(S3ObjectFactory.Provider.ObjectExists(S3ObjectFactory.GetInfo($"{objectKeyFromPath}/", true)));
            }
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
        /// </summary>
        /// <param name="path">Path</param>
        /// <param name="contents">Content to write.</param>
        public override void AppendAllText(string path, string contents)
        {
            string directoryName = CMS.IO.Path.GetDirectoryName(path);

            if (!CMS.IO.Directory.Exists(directoryName))
            {
                throw GetDirectoryNotFoundException(directoryName);
            }
            IS3ObjectInfo info = S3ObjectFactory.GetInfo(path);

            File.Provider.AppendTextToObject(info, contents);
            info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
        /// </summary>
        /// <param name="path">Path to file.</param>
        /// <param name="bytes">Bytes to write.</param>
        public override void WriteAllBytes(string path, byte[] bytes)
        {
            string directoryName = CMS.IO.Path.GetDirectoryName(path);

            if (!CMS.IO.Directory.Exists(directoryName))
            {
                throw GetDirectoryNotFoundException(directoryName);
            }
            var           memoryStream = new System.IO.MemoryStream(bytes);
            IS3ObjectInfo info         = S3ObjectFactory.GetInfo(path);

            Provider.PutDataFromStreamToObject(info, memoryStream);
            info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now));
        }
Exemplo n.º 5
0
 /// <summary>Opens an existing UTF-8 encoded text file for reading.</summary>
 /// <param name="path">Path to file</param>
 public override CMS.IO.StreamReader OpenText(string path)
 {
     if (this.ExistsInS3Storage(path))
     {
         System.IO.Stream objectContent = Provider
                                          .GetObjectContent(S3ObjectFactory.GetInfo(path), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 4096);
         return(objectContent == null ? null : CMS.IO.StreamReader.New(objectContent));
     }
     if (this.ExistsInFileSystem(path))
     {
         return(CMS.IO.StreamReader.New(System.IO.File.OpenText(path)));
     }
     throw GetFileNotFoundException(path);
 }
Exemplo n.º 6
0
        /// <summary>
        /// Returns the date and time the specified file or directory was last written to.
        /// </summary>
        /// <param name="path">Path to file.</param>
        public override DateTime GetLastWriteTime(string path)
        {
            if (!this.Exists(path))
            {
                throw GetFileNotFoundException(path);
            }
            IS3ObjectInfo info = S3ObjectFactory.GetInfo(path);

            if (Provider.ObjectExists(info))
            {
                return(S3ObjectInfoProvider.GetStringDateTime(info.GetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME)));
            }
            return(System.IO.File.GetLastAccessTime(path));
        }
Exemplo n.º 7
0
 /// <summary>Prepares files for import. Converts them to lower case.</summary>
 /// <param name="path">Path.</param>
 public override void PrepareFilesForImport(string path)
 {
     path = PathHelper.GetValidPath(path);
     foreach (string objects in this.Provider.GetObjectsList(path, ObjectTypeEnum.FilesAndDirectories, true, false, true))
     {
         IS3ObjectInfo info1          = S3ObjectFactory.GetInfo(PathHelper.GetObjectKeyFromPath(objects, false), true);
         string        lowerInvariant = info1.Key.ToLowerInvariant();
         if (lowerInvariant != info1.Key)
         {
             IS3ObjectInfo info2 = S3ObjectFactory.GetInfo(info1.Key, true);
             IS3ObjectInfo info3 = S3ObjectFactory.GetInfo(lowerInvariant, true);
             this.Provider.CopyObjects(info2, info3);
             this.Provider.DeleteObject(info2);
         }
     }
 }
Exemplo n.º 8
0
        /// <summary>
        /// Sets the date and time that the specified file was last written to.
        /// </summary>
        /// <param name="path">Path to file.</param>
        /// <param name="lastWriteTime">Last write time.</param>
        public override void SetLastWriteTime(string path, DateTime lastWriteTime)
        {
            if (!this.Exists(path))
            {
                throw GetFileNotFoundException(path);
            }
            IS3ObjectInfo info = S3ObjectFactory.GetInfo(path);

            if (Provider.ObjectExists(info))
            {
                info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(lastWriteTime));
            }
            else
            {
                throw new InvalidOperationException($"Cannot last write time to file '{path}' because is located only in application file system. \r\n                    This exception typically occurs when file system is mapped to Amazon S3 storage after the file or directory\r\n                    '{path}' was created in the local file system. To fix this issue move given file to Amazon S3 storage.");
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// Sets the specified FileAttributes  of the file on the specified path.
        /// </summary>
        /// <param name="path">Path to file.</param>
        /// <param name="fileAttributes">File attributes.</param>
        public override void SetAttributes(string path, CMS.IO.FileAttributes fileAttributes)
        {
            if (!this.Exists(path))
            {
                throw GetFileNotFoundException(path);
            }
            IS3ObjectInfo info = S3ObjectFactory.GetInfo(path);

            if (File.Provider.ObjectExists(info))
            {
                info.SetMetadata(S3ObjectInfoProvider.ATTRIBUTES, ValidationHelper.GetString(ValidationHelper.GetInteger(fileAttributes, 0), string.Empty), false);
                info.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now));
            }
            else
            {
                throw new InvalidOperationException($"Cannot set attributes to file '{path}' because it exists only in application file system. \r\n                    This exception typically occurs when file system is mapped to Amazon S3 storage after the file or directory\r\n                    '{path}' was created in the local file system. To fix this issue move given file to Amazon S3 storage.");
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Copies an existing file to a new file. Overwriting a file of the same name is allowed.
        /// </summary>
        /// <param name="sourceFileName">Path to source file.</param>
        /// <param name="destFileName">Path to destination file.</param>
        /// <param name="overwrite">If destination file should be overwritten.</param>
        public override void Copy(string sourceFileName, string destFileName, bool overwrite)
        {
            if (!this.Exists(sourceFileName))
            {
                throw GetFileNotFoundException(sourceFileName);
            }
            bool destExists = CMS.IO.File.Exists(destFileName);

            if (destExists && !overwrite)
            {
                return;
            }
            if (!StorageHelper.IsSameStorageProvider(sourceFileName, destFileName))
            {
                StorageHelper.CopyFileAcrossProviders(sourceFileName, destFileName);
            }
            else
            {
                IS3ObjectInfo sourceInfo = S3ObjectFactory.GetInfo(sourceFileName);
                IS3ObjectInfo destInfo   = S3ObjectFactory.GetInfo(destFileName);
                if (destExists)
                {
                    Provider.DeleteObject(destInfo);
                }
                if (Provider.ObjectExists(sourceInfo))
                {
                    Provider.CopyObjects(sourceInfo, destInfo);
                }
                else
                {
                    Provider.PutFileToObject(destInfo, sourceFileName);
                }
                IS3ObjectInfo destDirectoryInfo = S3ObjectFactory.GetInfo(CMS.IO.Path.GetDirectoryName(destFileName));
                destDirectoryInfo.Key = $"{destDirectoryInfo.Key}/";
                if (!Provider.ObjectExists(destDirectoryInfo))
                {
                    Provider.CreateEmptyObject(destDirectoryInfo);
                }
                var now = DateTime.Now;
                destDirectoryInfo.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(now));
                destInfo.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(now), false);
                destInfo.SetMetadata(S3ObjectInfoProvider.CREATION_TIME, S3ObjectInfoProvider.GetDateTimeString(now));
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Opens a text file, reads all lines of the file, and then closes the file.
        /// </summary>
        /// <param name="path">Path to file.</param>
        public override string ReadAllText(string path)
        {
            if (!this.Exists(path))
            {
                throw GetFileNotFoundException(path);
            }
            IS3ObjectInfo info = S3ObjectFactory.GetInfo(path);

            if (!Provider.ObjectExists(info))
            {
                return(System.IO.File.ReadAllText(path));
            }
            using (CMS.IO.StreamReader streamReader =
                       CMS.IO.StreamReader.New(Provider
                                               .GetObjectContent(info, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 4096)
                                               ))
            {
                return(streamReader.ReadToEnd());
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
        /// </summary>
        /// <param name="path">Path to file.</param>
        public override byte[] ReadAllBytes(string path)
        {
            if (!this.Exists(path))
            {
                throw GetFileNotFoundException(path);
            }
            IS3ObjectInfo info = S3ObjectFactory.GetInfo(path);

            if (!File.Provider.ObjectExists(info))
            {
                return(System.IO.File.ReadAllBytes(path));
            }
            System.IO.Stream objectContent = Provider
                                             .GetObjectContent(info, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read, 4096);
            byte[] buffer = new byte[objectContent.Length];
            objectContent.Seek(0L, SeekOrigin.Begin);
            objectContent.Read(buffer, 0, ValidationHelper.GetInteger(objectContent.Length, 0));
            objectContent.Close();
            return(buffer);
        }
Exemplo n.º 13
0
        /// <summary>Initializes file stream object.</summary>
        protected virtual void InitFileStream()
        {
            string path = CMS.IO.Path.Combine(PathHelper.TempPath, PathHelper.GetRelativePath(this.mPath));

            Directory.CreateDiskDirectoryStructure(path);
            this.obj = S3ObjectFactory.GetInfo(this.mPath);
            if (this.Provider.ObjectExists(this.obj))
            {
                if (this.fileMode == CMS.IO.FileMode.CreateNew)
                {
                    throw new Exception("Cannot create a new file, the file is already exist.");
                }
                this.fsTemp = (System.IO.FileStream) this.Provider.GetObjectContent(this.obj, (System.IO.FileMode) this.fileMode,
                                                                                    (System.IO.FileAccess) this.fileAccess, (System.IO.FileShare) this.fileShare, this.bufferSize);
                if (this.fileMode == CMS.IO.FileMode.Append)
                {
                    this.fsTemp.Position = this.fsTemp.Length;
                }
            }
            else
            {
                if (System.IO.File.Exists(this.mPath))
                {
                    this.fsStream = new System.IO.FileStream(this.mPath, (System.IO.FileMode) this.fileMode,
                                                             (System.IO.FileAccess) this.fileAccess, (System.IO.FileShare) this.fileShare, this.bufferSize);
                }
            }
            if (this.fsTemp != null ||
                this.fsStream != null)
            {
                return;
            }
            try
            {
                this.fsTemp = new System.IO.FileStream(path, System.IO.FileMode.Create,
                                                       System.IO.FileAccess.ReadWrite, (System.IO.FileShare) this.fileShare, this.bufferSize);
            }
            catch (FileNotFoundException)
            {
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Creates all directories and subdirectories as specified by path.
        /// </summary>
        /// <param name="path">Path to create.</param>
        public override CMS.IO.DirectoryInfo CreateDirectory(string path)
        {
            path = PathHelper.GetValidPath(path);
            if (this.Exists(path))
            {
                return(new DirectoryInfo(path));
            }
            IS3ObjectInfo info = S3ObjectFactory.GetInfo(path);

            info.Key = $"{info.Key}/";
            this.Provider.CreateEmptyObject(info);
            DirectoryInfo directoryInfo = new DirectoryInfo(path)
            {
                CreationTime = DateTime.Now,
                Exists       = true,
                FullName     = path
            };

            directoryInfo.LastWriteTime = directoryInfo.CreationTime;
            directoryInfo.Name          = System.IO.Path.GetFileName(path);
            return(directoryInfo);
        }
Exemplo n.º 15
0
 /// <summary>Initializes new instance of FileInfo class.</summary>
 /// <param name="filename">File name.</param>
 public FileInfo(string filename)
 {
     this.mExtension = CMS.IO.Path.GetExtension(filename);
     this.mFullName  = filename;
     this.mName      = CMS.IO.Path.GetFileName(filename);
     this.mExists    = CMS.IO.File.Exists(filename);
     this.IsReadOnly = false;
     this.Attributes = CMS.IO.FileAttributes.Normal;
     this.obj        = S3ObjectFactory.GetInfo(filename);
     if (!this.Provider.ObjectExists(this.obj))
     {
         if (System.IO.File.Exists(filename))
         {
             this.mSystemInfo = new System.IO.FileInfo(filename);
         }
     }
     else
     {
         this.mExists           = true;
         this.existsInS3Storage = true;
     }
     this.InitCMSValues();
 }
Exemplo n.º 16
0
        /// <summary>
        /// Deletes the specified file. An exception is not thrown if the specified file does not exist.
        /// </summary>
        /// <param name="path">Path to file</param>
        public override void Delete(string path)
        {
            if (!this.Exists(path))
            {
                throw GetFileNotFoundException(path);
            }
            IS3ObjectInfo info1 = S3ObjectFactory.GetInfo(path);

            if (Provider.ObjectExists(info1))
            {
                Provider.DeleteObject(info1);
                IS3ObjectInfo info2 = S3ObjectFactory.GetInfo(CMS.IO.Path.GetDirectoryName(path));
                info2.Key = $"{info2.Key}/";
                if (!Provider.ObjectExists(info2))
                {
                    Provider.CreateEmptyObject(info2);
                }
                info2.SetMetadata(S3ObjectInfoProvider.LAST_WRITE_TIME, S3ObjectInfoProvider.GetDateTimeString(DateTime.Now));
            }
            else
            {
                throw new InvalidOperationException($"File '{path}' cannot be deleted because it exists only in application file system. \r\n                    This exception typically occurs when file system is mapped to Amazon S3 storage after the file or directory\r\n                    '{path}' was created in the local file system. To fix this issue remove specified file or directory.");
            }
        }
Exemplo n.º 17
0
 /// <summary>
 /// Deletes an empty directory and, if indicated, any subdirectories and files in the directory.
 /// </summary>
 /// <param name="path">Path to directory</param>
 /// <param name="recursive">Deletes all sub directories in given path.</param>
 public override void Delete(string path, bool recursive)
 {
     if (ExistsInS3Storage(path))
     {
         if (recursive)
         {
             this.DeleteInternal(path, true);
         }
         else
         {
             if (this.Provider.GetObjectsList(path, ObjectTypeEnum.FilesAndDirectories, false, true, true).Count != 0)
             {
                 throw new InvalidOperationException("Directory is not empty.");
             }
             this.Provider.DeleteObject(S3ObjectFactory.GetInfo(path));
         }
         if (path.StartsWith(CurrentDirectory, StringComparison.OrdinalIgnoreCase))
         {
             path = path.Substring(CurrentDirectory.Length);
             try
             {
                 System.IO.Directory.Delete(System.IO.Path.Combine(PathHelper.TempPath, path));
                 System.IO.Directory.Delete(System.IO.Path.Combine(PathHelper.CachePath, path));
             }
             catch (IOException) { }
         }
     }
     else
     {
         if (ExistsInFileSystem(path))
         {
             throw new InvalidOperationException($"Cannot delete path '{path}' because it's not in Amazon S3 storage and it exists only in local file system.\r\n                    This exception typically occurs when file system is mapped to Amazon S3 storage after the file or directory\r\n                    '{path}' was created in the local file system. To fix this issue remove specified file or directory.");
         }
         throw new DirectoryNotFoundException($"Path '{path}' does not exist.");
     }
 }
Exemplo n.º 18
0
 /// <summary>Returns whether given file exists in S3 storage.</summary>
 /// <param name="path">Path to file.</param>
 private bool ExistsInS3Storage(string path)
 {
     return(Provider.ObjectExists(S3ObjectFactory.GetInfo(path)));
 }
Exemplo n.º 19
0
 /// <summary>Creates or opens a file for writing UTF-8 encoded text.</summary>
 /// <param name="path">Path to file.</param>
 public override CMS.IO.StreamWriter CreateText(string path)
 {
     S3ObjectFactory.GetInfo(path);
     return(CMS.IO.StreamWriter.New(this.GetFileStream(path, CMS.IO.FileMode.Create)));
 }