Пример #1
0
 public async ValueTask <IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null)
 {
     using (var stream = new SyncMemoryStream(data, 0, data.Length))
     {
         return(await this.SaveAsync(stream, file, contentType, overwritePolicy, metadata));
     }
 }
Пример #2
0
 public async ValueTask <IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always)
 {
     using (var stream = new MemoryStream(data, 0, data.Length))
     {
         return(await this.SaveAsync(stream, file, contentType, overwritePolicy));
     }
 }
        public Task SaveExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file, FileExtendedProperties extendedProperties)
        {
            var extendedPropertiesPath = this.GetExtendedPropertiesPath(storeAbsolutePath, file);
            var toStore = JsonConvert.SerializeObject(extendedProperties);

            File.WriteAllText(extendedPropertiesPath, toStore);
            return(Task.FromResult(0));
        }
Пример #4
0
        public async Task<IFileReference> AddMetadataAsync(IPrivateFileReference file, IDictionary<string, string> metadata)
        {
            var fileReference = await InternalGetAsync(file, false);

            fileReference.AddMetadataAsync(metadata);

            return fileReference;
        }
Пример #5
0
        public Task <IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType)
        {
            var fileReference = InternalGetOrCreateAsync(file);

            EnsurePathExists(fileReference.FileSystemPath);
            File.WriteAllBytes(fileReference.FileSystemPath, data);
            return(Task.FromResult((IFileReference)fileReference));
        }
Пример #6
0
 public async Task<IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType)
 {
     var blockBlob = this.container.Value.GetBlockBlobReference(file.Path);
     await blockBlob.UploadFromStreamAsync(data);
     blockBlob.Properties.ContentType = contentType;
     blockBlob.Properties.CacheControl = "max-age=300, must-revalidate";
     await blockBlob.SetPropertiesAsync();
     return new Internal.AzureFileReference(blockBlob);
 }
Пример #7
0
        private Internal.FileSystemFileReference InternalGetAsync(IPrivateFileReference file)
        {
            var reference = InternalGetOrCreateAsync(file);

            if (File.Exists(reference.FileSystemPath))
            {
                return(reference);
            }
            return(null);
        }
        private string GetExtendedPropertiesPath(string storeAbsolutePath, IPrivateFileReference file)
        {
            var fullPath  = Path.GetFullPath(storeAbsolutePath).TrimEnd(Path.DirectorySeparatorChar);
            var rootPath  = Path.GetDirectoryName(fullPath);
            var storeName = Path.GetFileName(fullPath);

            var extendedPropertiesPath = Path.Combine(rootPath, string.Format(this.options.FolderNameFormat, storeName), file.Path + ".json");

            this.EnsurePathExists(extendedPropertiesPath);
            return(extendedPropertiesPath);
        }
Пример #9
0
        public async Task <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType)
        {
            var fileReference = InternalGetOrCreateAsync(file);

            EnsurePathExists(fileReference.FileSystemPath);
            using (var fileStream = File.Open(fileReference.FileSystemPath, FileMode.Create, FileAccess.Write))
            {
                await data.CopyToAsync(fileStream);
            }

            return(fileReference);
        }
Пример #10
0
        public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType,
                                                          OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null)
        {
            var uploadBlob = true;
            var blockBlob  = _container.Value.GetBlockBlobReference(file.Path);
            var blobExists = await blockBlob.ExistsAsync();

            if (blobExists)
            {
                if (overwritePolicy == OverwritePolicy.Never)
                {
                    throw new FileAlreadyExistsException(Name, file.Path);
                }

                await blockBlob.FetchAttributesAsync();

                if (overwritePolicy == OverwritePolicy.IfContentModified)
                {
                    using (var md5 = MD5.Create())
                    {
                        data.Seek(0, SeekOrigin.Begin);
                        var contentMd5 = Convert.ToBase64String(md5.ComputeHash(data));
                        data.Seek(0, SeekOrigin.Begin);
                        uploadBlob = contentMd5 != blockBlob.Properties.ContentMD5;
                    }
                }
            }

            if (metadata != null)
            {
                foreach (var kvp in metadata)
                {
                    blockBlob.Metadata.Add(kvp.Key, kvp.Value);
                }
            }

            if (uploadBlob)
            {
                await blockBlob.UploadFromStreamAsync(data);
            }

            var reference = new AzureFileReference(blockBlob, true);

            if (reference.Properties.ContentType == contentType)
            {
                return(reference);
            }

            reference.Properties.ContentType = contentType;
            await reference.SavePropertiesAsync();

            return(reference);
        }
        public ValueTask <FileExtendedProperties> GetExtendedPropertiesAsync(string storeAbsolutePath,
                                                                             IPrivateFileReference file)
        {
            var extendedPropertiesPath = GetExtendedPropertiesPath(storeAbsolutePath, file);

            if (!File.Exists(extendedPropertiesPath))
            {
                return(new ValueTask <FileExtendedProperties>(new FileExtendedProperties()));
            }

            var content = File.ReadAllText(extendedPropertiesPath);

            return(new ValueTask <FileExtendedProperties>(
                       JsonConvert.DeserializeObject <FileExtendedProperties>(content)));
        }
Пример #12
0
        public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType,
                                                          OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null)
        {
            var fileReference = await InternalGetAsync(file, withMetadata : true, checkIfExists : false);

            var fileExists = File.Exists(fileReference.FileSystemPath);

            if (fileExists)
            {
                if (overwritePolicy == OverwritePolicy.Never)
                {
                    throw new FileAlreadyExistsException(Name, file.Path);
                }
            }

            var properties = fileReference.Properties as FileSystemFileProperties;

            var(ETag, ContentMd5) = ComputeHashes(data);

            if (!fileExists ||
                overwritePolicy == OverwritePolicy.Always ||
                (overwritePolicy == OverwritePolicy.IfContentModified &&
                 properties.ContentMd5 != ContentMd5))
            {
                EnsurePathExists(fileReference.FileSystemPath);

                using (var fileStream = File.Open(fileReference.FileSystemPath, FileMode.Create, FileAccess.Write))
                {
                    await data.CopyToAsync(fileStream);
                }
            }

            properties.ContentType                   = contentType;
            properties.ExtendedProperties.ETag       = ETag;
            properties.ExtendedProperties.ContentMd5 = ContentMd5;

            if (metadata != null)
            {
                foreach (var kvp in metadata)
                {
                    properties.Metadata.Add(kvp.Key, kvp.Value);
                }
            }

            await fileReference.SavePropertiesAsync();

            return(fileReference);
        }
Пример #13
0
        public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always)
        {
            var uploadBlob = true;
            var blockBlob  = this.container.Value.GetBlockBlobReference(file.Path);
            var blobExists = await blockBlob.ExistsAsync();

            if (blobExists)
            {
                if (overwritePolicy == OverwritePolicy.Never)
                {
                    throw new Exceptions.FileAlreadyExistsException(this.Name, file.Path);
                }

                await blockBlob.FetchAttributesAsync();

                if (overwritePolicy == OverwritePolicy.IfContentModified)
                {
                    using (var md5 = MD5.Create())
                    {
                        data.Seek(0, SeekOrigin.Begin);
                        var contentMD5 = Convert.ToBase64String(md5.ComputeHash(data));
                        data.Seek(0, SeekOrigin.Begin);
                        uploadBlob = (contentMD5 != blockBlob.Properties.ContentMD5);
                    }
                }
            }

            if (uploadBlob)
            {
                await blockBlob.UploadFromStreamAsync(data);
            }

            var reference = new Internal.AzureFileReference(blockBlob, withMetadata: true);

            if (reference.Properties.ContentType != contentType)
            {
                reference.Properties.ContentType = contentType;
                await reference.SavePropertiesAsync();
            }

            return(reference);
        }
Пример #14
0
        private async Task<Internal.AzureFileReference> InternalGetAsync(IPrivateFileReference file, bool withMetadata)
        {
            var azureFile = file as Internal.AzureFileReference;
            if (azureFile != null)
            {
                return azureFile;
            }

            try
            {
                var blob = await this.container.Value.GetBlobReferenceFromServerAsync(file.Path);
                return new Internal.AzureFileReference(file.Path, blob);
            }
            catch (StorageException storageException)
            {
                if (storageException.RequestInformation.HttpStatusCode == 404)
                {
                    return null;
                }
                throw;
            }
        }
Пример #15
0
        public async ValueTask <IFileReference> SaveAsync(Stream data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always)
        {
            var fileReference = await this.InternalGetAsync(file, withMetadata : true, checkIfExists : false);

            var fileExists = File.Exists(fileReference.FileSystemPath);

            if (fileExists)
            {
                if (overwritePolicy == OverwritePolicy.Never)
                {
                    throw new Exceptions.FileAlreadyExistsException(this.Name, file.Path);
                }
            }

            var properties = fileReference.Properties as Internal.FileSystemFileProperties;
            var hashes     = ComputeHashes(data);

            if (!fileExists ||
                overwritePolicy == OverwritePolicy.Always ||
                (overwritePolicy == OverwritePolicy.IfContentModified && properties.ContentMD5 != hashes.ContentMD5))
            {
                this.EnsurePathExists(fileReference.FileSystemPath);

                using (var fileStream = File.Open(fileReference.FileSystemPath, FileMode.Create, FileAccess.Write))
                {
                    await data.CopyToAsync(fileStream);
                }
            }

            properties.ContentType                   = contentType;
            properties.ExtendedProperties.ETag       = hashes.ETag;
            properties.ExtendedProperties.ContentMD5 = hashes.ContentMD5;

            await fileReference.SavePropertiesAsync();

            return(fileReference);
        }
Пример #16
0
 public ValueTask <FileExtendedProperties> GetExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file)
 {
     throw new InvalidOperationException("There is no FileSystem extended properties provider. Add ");
 }
Пример #17
0
 public Task SaveExtendedPropertiesAsync(string storeAbsolutePath, IPrivateFileReference file, FileExtendedProperties extendedProperties)
 {
     throw new InvalidOperationException("There is no FileSystem extended properties provider.");
 }
Пример #18
0
 public ValueTask <IFileReference> SaveAsync(byte[] data, IPrivateFileReference file, string contentType, OverwritePolicy overwritePolicy = OverwritePolicy.Always, IDictionary <string, string> metadata = null) => this.innerStore.SaveAsync(data, file, contentType, overwritePolicy);
Пример #19
0
 public ValueTask <Stream> ReadAsync(IPrivateFileReference file) => this.innerStore.ReadAsync(file);
Пример #20
0
 private ValueTask <FileSystemFileReference> InternalGetAsync(IPrivateFileReference file,
                                                              bool withMetadata = false, bool checkIfExists = true)
 {
     return(InternalGetAsync(file.Path, withMetadata, checkIfExists));
 }
Пример #21
0
        public async ValueTask <byte[]> ReadAllBytesAsync(IPrivateFileReference file)
        {
            var fileReference = await this.InternalGetAsync(file);

            return(await fileReference.ReadAllBytesAsync());
        }
Пример #22
0
        public async Task DeleteAsync(IPrivateFileReference file)
        {
            var fileReference = await this.InternalGetAsync(file);

            await fileReference.DeleteAsync();
        }
Пример #23
0
 public async ValueTask <IFileReference> GetAsync(IPrivateFileReference file, bool withMetadata)
 {
     return(await this.InternalGetAsync(file, withMetadata));
 }
Пример #24
0
 public Task DeleteAsync(IPrivateFileReference file) => this.innerStore.DeleteAsync(file);
Пример #25
0
        public async ValueTask <Stream> ReadAsync(IPrivateFileReference file)
        {
            var fileReference = await this.InternalGetAsync(file);

            return(await fileReference.ReadInMemoryAsync());
        }
Пример #26
0
 public ValueTask <IFileReference> GetAsync(IPrivateFileReference file, bool withMetadata) => this.innerStore.GetAsync(file, withMetadata);
Пример #27
0
        public async ValueTask <string> ReadAllTextAsync(IPrivateFileReference file)
        {
            var fileReference = await this.InternalGetAsync(file);

            return(await fileReference.ReadAllTextAsync());
        }
Пример #28
0
 public ValueTask <byte[]> ReadAllBytesAsync(IPrivateFileReference file) => this.innerStore.ReadAllBytesAsync(file);
Пример #29
0
 private ValueTask <Internal.AzureFileReference> InternalGetAsync(IPrivateFileReference file, bool withMetadata = false)
 {
     return(this.InternalGetAsync(new Uri(file.Path, UriKind.Relative), withMetadata));
 }
Пример #30
0
 public ValueTask <string> ReadAllTextAsync(IPrivateFileReference file) => this.innerStore.ReadAllTextAsync(file);