Example #1
0
        private async Task <bool> ReadPart(MultipartFormDataStreamProvider provider)
        {
            try
            {
                return(await Request.Content.ReadAsMultipartAsync(provider).ContinueWith <bool>(x =>
                {
                    if (x.IsFaulted || x.IsCanceled)
                    {
                        return false;
                    }

                    ResumableConfiguration configuration = GetUploadConfiguration(x.Result);
                    int chunkNumber = GetChunkNumber(x.Result);

                    // Rename generated file
                    MultipartFileData chunk = x.Result.FileData[0]; // Only one file in multipart message
                    RenameChunk(chunk, chunkNumber, configuration.Identifier);

                    // Assemble chunks into single file if they're all here
                    TryAssembleFile(configuration);
                    return true;
                }));
            }
            catch
            {
                return(false);
            }
        }
Example #2
0
        private void TryAssembleFile(ResumableConfiguration configuration)
        {
            if (AllChunksAreHere(configuration))
            {
                // Create a single file
                var path      = ConsolidateFile(configuration);
                var extension = configuration.FileName.Split('.');
                var fileName  = Guid.NewGuid();
                // Rename consolidated with original name of upload
                RenameFile(path, Path.Combine(root, fileName.ToString() + (extension.Length > 1 ? "." + extension[extension.Length - 1] : "")));

                // Delete chunk files
                DeleteChunks(configuration);

                var request = new InsertFileRequest()
                {
                    CreatedDate = DateTime.Now,
                    IdUser      = int.Parse(User.GetUserId()),
                    Name        = configuration.FileName,
                    Size        = configuration.Size,
                    NomeInterno = fileName
                };

                JsonConvert.DeserializeObject <InsertFileResponse>
                    (ServiceApiUtil.ApiResponse("api/file/Insert", "POST", request));
            }
        }
Example #3
0
 private ResumableConfiguration GetUploadConfiguration(MultipartFormDataStreamProvider provider)
 {
     return(ResumableConfiguration.Create(identifier: GetId(provider),
                                          filename: GetFileName(provider),
                                          chunks: GetTotalChunks(provider),
                                          size: GetTotalSize(provider)));
 }
 private void DeleteChunks(ResumableConfiguration configuration)
 {
     for (int chunkNumber = 1; chunkNumber <= configuration.Chunks; chunkNumber++)
     {
         var chunkFileName = GetChunkFileName(chunkNumber, configuration.Identifier);
         File.Delete(chunkFileName);
     }
 }
 private bool AllChunksAreHere(ResumableConfiguration configuration)
 {
     for (int chunkNumber = 1; chunkNumber <= configuration.Chunks; chunkNumber++)
     {
         if (!ChunkIsHere(chunkNumber, configuration.Identifier))
         {
             return(false);
         }
     }
     return(true);
 }
        private void TryAssembleFile(ResumableConfiguration configuration)
        {
            if (AllChunksAreHere(configuration))
            {
                // Create a single file
                var path = ConsolidateFile(configuration);

                // Rename consolidated with original name of upload
                RenameFile(path, Path.Combine(root, configuration.FileName));

                // Delete chunk files
                DeleteChunks(configuration);
            }
        }
        private string ConsolidateFile(ResumableConfiguration configuration)
        {
            var path = GetFilePath(configuration);

            using (var destStream = File.Create(path, 15000))
            {
                for (int chunkNumber = 1; chunkNumber <= configuration.Chunks; chunkNumber++)
                {
                    var chunkFileName = GetChunkFileName(chunkNumber, configuration.Identifier);
                    using (var sourceStream = File.OpenRead(chunkFileName))
                    {
                        sourceStream.CopyTo(destStream);
                    }
                }
                destStream.Close();
            }

            return(path);
        }
        private async Task <bool> readPart(MultipartFormDataStreamProvider provider)
        {
            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                ResumableConfiguration configuration = GetUploadConfiguration(provider);
                int chunkNumber = GetChunkNumber(provider);

                // Rename generated file
                MultipartFileData chunk = provider.FileData[0];                 // Only one file in multipart message
                RenameChunk(chunk, chunkNumber, configuration.Identifier);

                // Assemble chunks into single file if they're all here
                TryAssembleFile(configuration);
                return(true);
            }
            catch {
                return(false);
            }
        }
Example #9
0
        private void TryAssembleFile(ResumableConfiguration configuration)
        {
            if (AllChunksAreHere(configuration))
            {
                // Create a single file
                var path = ConsolidateFile(configuration);

                // Rename consolidated with original name of upload
                RenameFile(path, Path.Combine(root, configuration.FileName));

                // Delete chunk files
                DeleteChunks(configuration);
                var request = new Domain.Arguments.File.InsertFileRequest()
                {
                    CreatedDate = DateTime.Now,
                    IdUser      = 1,
                    Name        = "file.name",
                    Size        = 12312
                };
                var response = _serviceFile.InsertFile(request);
            }
        }
 private string GetFilePath(ResumableConfiguration configuration)
 {
     return(Path.Combine(root, configuration.Identifier));
 }