コード例 #1
0
        public virtual Task <IStoredItemInformation> GetInformationAsync(string storagePointer,
                                                                         CancellationToken cancellationToken = default)
        {
            ApplyBaseDirectoryToStoragePointer(ref storagePointer);

            if (!TryGetStoredItemType(storagePointer, out var type) || type == null)
            {
                return(null);
            }

            var fileInfo      = new FileInfo(storagePointer);
            var directoryPath = Path.GetDirectoryName(storagePointer);

            IStoredItemInformation result = new StoredItemInformation
            {
                StoredType            = (StoredItemType)type,
                DirectoryPath         = directoryPath,
                Name                  = fileInfo.Name,
                StoragePointer        = fileInfo.FullName,
                Size                  = fileInfo.Length,
                CreatedTimestamp      = fileInfo.CreationTimeUtc,
                LastModifiedTimestamp = fileInfo.LastWriteTimeUtc
            };

            return(Task.FromResult(result));
        }
コード例 #2
0
        public async Task <IStoredItemInformation> GetInformationAsync(string storagePointer, CancellationToken cancellationToken = new CancellationToken())
        {
            using (var client = GetClient())
            {
                if (client == null)
                {
                    throw Errors.UnknownException;
                }
                await client.ConnectAsync(cancellationToken);

                if (!await client.FileExistsAsync(GetFullPath(storagePointer), cancellationToken))
                {
                    return(null);
                }

                var info = await client.GetObjectInfoAsync(GetFullPath(storagePointer), true, cancellationToken);

                var result = new StoredItemInformation
                {
                    CreatedTimestamp      = info?.Created,
                    DirectoryPath         = GetFullPath(storagePointer),
                    LastModifiedTimestamp = info?.Modified,
                    Name           = info?.Name,
                    Size           = info?.Size,
                    StoragePointer = storagePointer,
                    StoredType     = info?.Type == FtpFileSystemObjectType.Directory
                        ? StoredItemType.Directory
                        : StoredItemType.File
                };

                return(result);
            }
        }
        public async Task <IStoredItemInformation> GetInformationAsync(string storagePointer, CancellationToken cancellationToken = new CancellationToken())
        {
            var type = await TryGetStoredItemTypeAsync(storagePointer, cancellationToken);

            if (type == null)
            {
                throw Core.Exceptions.Errors.FileNotFoundException;
            }

            var info = GetClient(storagePointer);

            var props = type == StoredItemType.File
                ? await info.GetPropertiesAsync(new BlobRequestConditions(), cancellationToken)
                : null;

            var pathParts =
                storagePointer
                .Split(DirectorySeparatorCharacter)
                .Where(w => !string.IsNullOrWhiteSpace(w))
                .ToList();

            var name = pathParts.Last();

            pathParts.Remove(name);
            var dirPath = string.Join(DirectorySeparatorCharacter.ToString(), pathParts);

            var result = new StoredItemInformation
            {
                CreatedTimestamp      = props?.Value.CreatedOn.UtcDateTime,
                DirectoryPath         = dirPath,
                LastModifiedTimestamp = props?.Value.LastModified.UtcDateTime,
                Name           = name,
                Size           = props?.Value.ContentLength,
                StoragePointer = info.Name,
                StoredType     = (StoredItemType)type
            };

            return(result);
        }
コード例 #4
0
        public virtual async Task <IStoredItemInformation> GetInformationAsync(string storagePointer,
                                                                               CancellationToken cancellationToken = default)
        {
            try
            {
                var result = new StoredItemInformation();
                using (var s3Client = new AmazonS3Client(GetAmazonCredentials(), GetBucketInfo().GetRegionEndpoint()))
                {
                    try
                    {
                        var omInfo = await s3Client
                                     .GetObjectMetadataAsync(GetBucketInfo().Name, storagePointer, cancellationToken)
                                     .ConfigureAwait(false);

                        result.StoredType            = StoredItemType.File;
                        result.Size                  = omInfo.ContentLength;
                        result.LastModifiedTimestamp = omInfo.LastModified.ToUniversalTime();
                        result.StoragePointer        = storagePointer;
                    }
                    catch (AmazonS3Exception s3Exception) when(s3Exception.StatusCode == HttpStatusCode.NotFound)
                    {
                        storagePointer = storagePointer.EndsWith(DirectorySeparatorCharacter.ToString())
                            ? storagePointer
                            : storagePointer + DirectorySeparatorCharacter;

                        var loInfo = await s3Client
                                     .ListObjectsAsync(GetBucketInfo().Name, storagePointer, cancellationToken)
                                     .ConfigureAwait(false);

                        if (!loInfo.S3Objects.Any())
                        {
                            throw;
                        }

                        result.StoredType = StoredItemType.Directory;
                        result.Size       = null;
                    }
                }

                var pathParts =
                    storagePointer
                    .Split(DirectorySeparatorCharacter)
                    .Where(part => !string.IsNullOrWhiteSpace(part))
                    .ToList();

                var name = pathParts.Last();
                pathParts.Remove(name);
                var dirPath = string.Join(DirectorySeparatorCharacter.ToString(), pathParts);

                if (result.StoredType == StoredItemType.Directory)
                {
                    name += DirectorySeparatorCharacter;
                }

                result.DirectoryPath    = dirPath;
                result.Name             = name;
                result.CreatedTimestamp = null;

                return(result);
            }
            catch (AmazonS3Exception s3Exception) when(s3Exception.StatusCode == HttpStatusCode.NotFound)
            {
                throw Errors.FileNotFoundException;
            }
            catch (Exception)
            {
                throw Errors.UnknownException;
            }
        }