Exemple #1
0
        /// <summary>
        /// Store a file at a path.
        /// </summary>
        /// <param name="path">Storage path.</param>
        /// <param name="fileHandle">File to store.</param>
        public void PutFile(string path, AwsS3FileHandle fileHandle)
        {
            if (IsDirectoryPath(path))
            {
                throw new InvalidOperationException(string.Format("cannot put a file at a path with a trailing {0}", _config.Delimiter));
            }
            var p = _rootPlug.AtPath(path);

            if (fileHandle.TimeToLive.HasValue)
            {
                var expiration = fileHandle.Expiration ?? DateTime.UtcNow.Add(fileHandle.TimeToLive.Value);
                p = p.WithHeader(EXPIRE, expiration.ToEpoch().ToString())
                    .WithHeader(TTL, fileHandle.TimeToLive.Value.TotalSeconds.ToString());
                _expirationEntries.SetOrUpdate(path, expiration, fileHandle.TimeToLive.Value);
            }
            if (!string.IsNullOrEmpty(fileHandle.CacheControl))
            {
                p = p.WithHeader(CACHE_CONTROL, fileHandle.CacheControl);
            }
            var request  = DreamMessage.Ok(fileHandle.MimeType, fileHandle.Size, fileHandle.Stream);
            var response = p.Put(request, new Result <DreamMessage>()).Wait();

            if (response.IsSuccessful)
            {
                return;
            }
            throw new DreamResponseException(response);
        }
Exemple #2
0
        private AwsS3DataInfo GetFile(string path, bool head)
        {
            var response = _rootPlug.AtPath(path).InvokeEx(head ? "HEAD" : "GET", DreamMessage.Ok(), new Result <DreamMessage>()).Wait();

            if (response.Status == DreamStatus.NotFound)
            {
                response.Close();
                return(null);
            }
            if (!response.IsSuccessful)
            {
                response.Memorize(new Result()).Wait();
                throw new DreamResponseException(response);
            }

            // got a file
            var expireEpoch = response.Headers[EXPIRE];
            var expiration  = string.IsNullOrEmpty(expireEpoch) ? (DateTime?)null : DateTimeUtil.FromEpoch(SysUtil.ChangeType <uint>(expireEpoch));
            var ttlString   = response.Headers[TTL];
            var ttl         = string.IsNullOrEmpty(ttlString) ? (TimeSpan?)null : TimeSpan.FromSeconds(SysUtil.ChangeType <double>(ttlString));

            if (expiration.HasValue && ttl.HasValue)
            {
                if (DateTime.UtcNow > expiration)
                {
                    // lazy expiration
                    _log.DebugFormat("lazy expiration of {0}", path);
                    _expirationEntries.Delete(path);
                    response.Close();
                    return(null);
                }
                _expirationEntries.SetOrUpdate(path, expiration.Value, ttl.Value);
            }
            var filehandle = new AwsS3FileHandle {
                Expiration = expiration,
                TimeToLive = ttl,
                Size       = response.ContentLength,
                MimeType   = response.ContentType,
                Modified   = response.Headers.LastModified ?? DateTime.UtcNow,
                Stream     = head ? null : response.ToStream(),
            };

            if (head)
            {
                response.Close();
            }
            return(new AwsS3DataInfo(filehandle));
        }
Exemple #3
0
 /// <summary>
 /// Create a file data info instance.
 /// </summary>
 /// <param name="fileHandle">File info instance.</param>
 public AwsS3DataInfo(AwsS3FileHandle fileHandle)
 {
     _fileHandle = fileHandle;
 }
Exemple #4
0
 public void PutFile(string path, AwsS3FileHandle fileInfo)
 {
     Client.PutFile(path, fileInfo);
 }
Exemple #5
0
        private AwsS3DataInfo GetFile(string path, bool head)
        {
            var response = _rootPlug.AtPath(path).InvokeEx(head ? "HEAD" : "GET", DreamMessage.Ok(), new Result<DreamMessage>()).Wait();
            if(response.Status == DreamStatus.NotFound) {
                response.Close();
                return null;
            }
            if(!response.IsSuccessful) {
                response.Memorize(new Result()).Wait();
                throw new DreamResponseException(response);
            }

            // got a file
            var expireEpoch = response.Headers[EXPIRE];
            var expiration = string.IsNullOrEmpty(expireEpoch) ? (DateTime?)null : DateTimeUtil.FromEpoch(SysUtil.ChangeType<uint>(expireEpoch));
            var ttlString = response.Headers[TTL];
            var ttl = string.IsNullOrEmpty(ttlString) ? (TimeSpan?)null : TimeSpan.FromSeconds(SysUtil.ChangeType<double>(ttlString));
            if(expiration.HasValue && ttl.HasValue) {
                if(DateTime.UtcNow > expiration) {

                    // lazy expiration
                    _log.DebugFormat("lazy expiration of {0}", path);
                    _expirationEntries.Delete(path);
                    response.Close();
                    return null;
                }
                _expirationEntries.SetOrUpdate(path, expiration.Value, ttl.Value);
            }
            var filehandle = new AwsS3FileHandle {
                Expiration = expiration,
                TimeToLive = ttl,
                Size = response.ContentLength,
                MimeType = response.ContentType,
                Modified = response.Headers.LastModified ?? DateTime.UtcNow,
                Stream = head ? null : response.ToStream(),
            };
            if(head) {
                response.Close();
            }
            return new AwsS3DataInfo(filehandle);
        }
Exemple #6
0
 /// <summary>
 /// Store a file at a path.
 /// </summary>
 /// <param name="path">Storage path.</param>
 /// <param name="fileHandle">File to store.</param>
 public void PutFile(string path, AwsS3FileHandle fileHandle)
 {
     if(IsDirectoryPath(path)) {
         throw new InvalidOperationException(string.Format("cannot put a file at a path with a trailing {0}", _config.Delimiter));
     }
     var p = _rootPlug.AtPath(path);
     if(fileHandle.TimeToLive.HasValue) {
         var expiration = fileHandle.Expiration ?? DateTime.UtcNow.Add(fileHandle.TimeToLive.Value);
         p = p.WithHeader(EXPIRE, expiration.ToEpoch().ToInvariantString())
             .WithHeader(TTL, fileHandle.TimeToLive.Value.TotalSeconds.ToInvariantString());
         _expirationEntries.SetOrUpdate(path, expiration, fileHandle.TimeToLive.Value);
     }
     if(!string.IsNullOrEmpty(fileHandle.CacheControl)) {
         p = p.WithHeader(CACHE_CONTROL, fileHandle.CacheControl);
     }
     var request = DreamMessage.Ok(fileHandle.MimeType, fileHandle.Size, fileHandle.Stream);
     var response = p.Put(request, new Result<DreamMessage>()).Wait();
     if(response.IsSuccessful) {
         return;
     }
     throw new DreamResponseException(response);
 }
Exemple #7
0
 /// <summary>
 /// Create a file data info instance.
 /// </summary>
 /// <param name="fileHandle">File info instance.</param>
 public AwsS3DataInfo(AwsS3FileHandle fileHandle)
 {
     _fileHandle = fileHandle;
 }