コード例 #1
0
        public async Task UpdateFile(FileModel file, Stream content)
        {
            var filePath = GetFilePath(file);

            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Write))
                await content.CopyToAsync(fileStream);
        }
コード例 #2
0
 public Task DeleteFile(File file)
 {
     if (!fileToContentMap.Remove(file))
     {
         throw new InvalidOperationException("File to remove not found.");
     }
     return(Task.CompletedTask);
 }
コード例 #3
0
 public Task <Stream> ReadFile(File file)
 {
     if (!fileToContentMap.TryGetValue(file, out Stream content))
     {
         throw new InvalidOperationException("File to read not found.");
     }
     return(Task.FromResult(content));
 }
コード例 #4
0
 public Task SaveFile(File file, Stream content)
 {
     if (!fileToContentMap.TryAdd(file, content))
     {
         throw new InvalidOperationException("The file already exists.");
     }
     return(Task.CompletedTask);
 }
コード例 #5
0
 public Task UpdateFile(File file, Stream content)
 {
     if (!fileToContentMap.TryGetValue(file, out _))
     {
         throw new InvalidOperationException("File to update not found.");
     }
     fileToContentMap[file] = content;
     return(Task.CompletedTask);
 }
コード例 #6
0
        public Task DeleteFile(FileModel file)
        {
            var filePath = Path.Combine(rootPath, EscapeName(file.Owner.Username), EscapeName(file.FileName));

            File.Delete(filePath);

            // clear the parent directory if it's empty after deleting the file
            DeleteParentDirectoryIfEmpty(filePath);
            return(Task.CompletedTask);
        }
コード例 #7
0
 public Task <Stream> ReadFile(FileModel file)
 {
     try
     {
         var filePath = GetFilePath(file);
         return(ReadFile(filePath));
     }
     catch (FileNotFoundException ex)
     {
         throw new NotFoundException($"A file with id {file.Id} was not found in the storage.", ex);
     }
     catch (Exception ex) when(ex is UnauthorizedAccessException || ex is IOException)
     {
         throw new StorageAccessException("An error occured during accessing the file storage", ex);
     }
 }
コード例 #8
0
        public static async Task <FileService.Model.File> StreamFile(this HttpRequest request, string Id, string FilePath)
        {
            if (!MultipartRequestHelper.IsMultipartContentType(request.ContentType))
            {
                throw new Exception($"Expected a multipart request, but got {request.ContentType}");
            }

            // Used to accumulate all the form url encoded key value pairs in the
            // request.
            var formAccumulator = new KeyValueAccumulator();

            var boundary = MultipartRequestHelper.GetBoundary(
                MediaTypeHeaderValue.Parse(request.ContentType),
                _defaultFormOptions.MultipartBoundaryLengthLimit);
            var reader = new MultipartReader(boundary, request.Body);

            var section = await reader.ReadNextSectionAsync();

            FileService.Model.File FileInstance = new FileService.Model.File();

            while (section != null)
            {
                ContentDispositionHeaderValue contentDisposition;
                var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out contentDisposition);
                if (hasContentDispositionHeader)
                {
                    if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition))
                    {
                        var FileName         = contentDisposition.FileName.Value;
                        var FileNameSegments = FileName.Replace("\"", "").Replace("'", "").Split(".");
                        FilePath              = FilePath + "." + FileNameSegments[FileNameSegments.Length - 1];
                        FileInstance.Name     = FileName.Replace("\"", "").Replace("'", "");
                        FileInstance.Note     = "";
                        FileInstance.Location = FilePath;
                        FileInstance.UserId   = Id;
                        using (var targetStream = File.Create(FilePath))
                        {
                            await section.Body.CopyToAsync(targetStream);
                        }
                    }
                    else if (MultipartRequestHelper.HasFormDataContentDisposition(contentDisposition))
                    {
                        // Content-Disposition: form-data; name="key"
                        //
                        // value

                        // Do not limit the key name length here because the
                        // multipart headers length limit is already in effect.
                        var key      = HeaderUtilities.RemoveQuotes(contentDisposition.Name);
                        var encoding = GetEncoding(section);
                        using (var streamReader = new StreamReader(
                                   section.Body,
                                   encoding,
                                   detectEncodingFromByteOrderMarks: true,
                                   bufferSize: 1024,
                                   leaveOpen: true))
                        {
                            // The value length limit is enforced by MultipartBodyLengthLimit
                            var value = await streamReader.ReadToEndAsync();

                            if (String.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
                            {
                                value = String.Empty;
                            }
                            formAccumulator.Append(key.Value, value); // For .NET Core <2.0 remove ".Value" from key

                            if (formAccumulator.ValueCount > _defaultFormOptions.ValueCountLimit)
                            {
                                throw new InvalidDataException($"Form key count limit {_defaultFormOptions.ValueCountLimit} exceeded.");
                            }
                        }
                    }
                }

                // Drains any remaining section body that has not been consumed and
                // reads the headers for the next section.
                section = await reader.ReadNextSectionAsync();
            }

            // Bind form data to a model
            var formValueProvider = new FormValueProvider(
                BindingSource.Form,
                new FormCollection(formAccumulator.GetResults()),
                CultureInfo.CurrentCulture);

            return(FileInstance);
        }
コード例 #9
0
 public async Task DeleteFile(File file)
 {
     using (fileLockingService.CreateLock(file, currentUser.ToDomainUser()))
         await decorated.DeleteFile(file);
 }
コード例 #10
0
 public async Task <Stream> ReadFile(File file)
 {
     return(await decorated.ReadFile(file));
 }
コード例 #11
0
 public async Task UpdateFile(File file, Stream content)
 {
     using (fileLockingService.CreateLock(file, currentUser.ToDomainUser()))
         await decorated.UpdateFile(file, content);
 }
コード例 #12
0
 public async Task SaveFile(File file, Stream content)
 {
     await decorated.SaveFile(file, content);
 }
コード例 #13
0
 private string GetFilePath(FileModel file)
 {
     return(Path.Combine(rootPath, EscapeName(file.Owner.Username), EscapeName(file.FileName)));
 }
コード例 #14
0
 public bool Exists(File file)
 {
     return(fileToContentMap.ContainsKey(file));
 }