Ejemplo n.º 1
0
        public Task <bool> SaveFileAsync(string path, Stream stream, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (String.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            byte[] contents = ReadBytes(stream);
            if (contents.Length > MaxFileSize)
            {
                throw new ArgumentException(String.Format("File size {0} exceeds the maximum size of {1}.", contents.Length.ToFileSizeDisplay(), MaxFileSize.ToFileSizeDisplay()));
            }

            lock (_lock) {
                _storage[path] = Tuple.Create(new FileSpec {
                    Created  = SystemClock.UtcNow,
                    Modified = SystemClock.UtcNow,
                    Path     = path,
                    Size     = contents.Length
                }, contents);

                if (_storage.Count > MaxFiles)
                {
                    _storage.Remove(_storage.OrderByDescending(kvp => kvp.Value.Item1.Created).First().Key);
                }
            }

            return(Task.FromResult(true));
        }
Ejemplo n.º 2
0
        public bool SaveFile(string path, string contents)
        {
            if (String.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException("path");
            }

            if (contents.Length > MaxFileSize)
            {
                throw new ArgumentException(String.Format("File size {0} exceeds the maximum size of {1}.", contents.Length.ToFileSizeDisplay(), MaxFileSize.ToFileSizeDisplay()));
            }

            lock (_lock) {
                _storage[path] = Tuple.Create(new FileInfo {
                    Created  = DateTime.Now,
                    Modified = DateTime.Now,
                    Path     = path,
                    Size     = contents.Length
                }, contents);

                if (_storage.Count > MaxFiles)
                {
                    _storage.Remove(_storage.OrderByDescending(kvp => kvp.Value.Item1.Created).First().Key);
                }
            }

            return(true);
        }