Beispiel #1
0
        public async Task <IActionResult> UploadDone(int segmentsCount, string fileName, string contentType)
        {
            // use manifest to merge chunks
            await _swiftService.PutManifest(containerTempId, fileName);

            // copy chunks to new file and set some meta data info about the file (filename, contentype)
            await _swiftService.CopyObject(containerTempId, fileName, containerDemoId, fileName, new Dictionary <string, string>
            {
                { $"X-Object-Meta-{metaFileName}", fileName },
                { $"X-Object-Meta-{metaContentType}", contentType }
            });

            // cleanup temp
            await _swiftService.DeleteContainerWithContents(containerTempId);

            return(new JsonResult(new
            {
                Success = true
            }));
        }
        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));
        }