예제 #1
0
        public async Task <IActionResult> UploadChunk(int segment)
        {
            if (Request.Form.Files != null && Request.Form.Files.Count > 0)
            {
                var file     = Request.Form.Files[0];
                var fileName = file.GetFileName();

                using (var fileStream = file.OpenReadStream())
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await fileStream.CopyToAsync(memoryStream);

                        var resp = await _swiftService.PutObjectChunk(containerTempId, fileName, memoryStream.ToArray(), segment);

                        return(new JsonResult(new
                        {
                            ContentType = file.ContentType,
                            FileName = fileName ?? "demofile",
                            Status = resp.StatusCode,
                            Message = resp.Reason,
                            Success = resp.IsSuccess
                        }));
                    }
                }
            }

            return(new JsonResult(new
            {
                Success = false
            }));
        }
예제 #2
0
        public static async Task <SwiftBaseResponse> PutLargeObject(this ISwiftClient client, string containerId, string objectId, Stream stream, Dictionary <string, string> headers = null, Action <long, long> progress = null, long bufferSize = 1000000, bool checkIntegrity = false)
        {
            SwiftBaseResponse response = null;

            byte[] buffer = new byte[bufferSize];
            string containerTemp = "tmp_" + Guid.NewGuid().ToString("N");
            int    bytesRead, chunk = 0;

            response = await client.PutContainer(containerTemp).ConfigureAwait(false);

            if (!response.IsSuccess)
            {
                return(response);
            }

            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                using (MemoryStream tmpStream = new MemoryStream())
                {
                    tmpStream.Write(buffer, 0, bytesRead);
                    response = await client.PutObjectChunk(containerTemp, objectId, tmpStream.ToArray(), chunk).ConfigureAwait(false);
                }

                progress?.Invoke(chunk, bytesRead);

                if (!response.IsSuccess)
                {
                    // cleanup
                    await client.DeleteContainerWithContents(containerTemp).ConfigureAwait(false);

                    return(response);
                }

                chunk++;
            }

            Dictionary <string, string> integrityHeaders = null;

            if (checkIntegrity)
            {
                using (var md5 = MD5.Create())
                {
                    var eTag = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();

                    integrityHeaders = new Dictionary <string, string>()
                    {
                        { "ETag", eTag }
                    };
                }
            }


            // use manifest to merge chunks
            response = await client.PutManifest(containerTemp, objectId, integrityHeaders).ConfigureAwait(false);

            if (!response.IsSuccess)
            {
                // cleanup
                await client.DeleteContainerWithContents(containerTemp).ConfigureAwait(false);

                return(response);
            }

            // copy chunks to new file
            response = await client.CopyObject(containerTemp, objectId, containerId, objectId, headers).ConfigureAwait(false);

            if (!response.IsSuccess)
            {
                // cleanup
                await client.DeleteContainerWithContents(containerTemp).ConfigureAwait(false);

                return(response);
            }

            // cleanup temp
            return(await client.DeleteContainerWithContents(containerTemp).ConfigureAwait(false));
        }