Beispiel #1
0
        public ICreateLinkRequest BuildCreateLinkRequest(string collection, string relativePath, IFileFingerprint fileFingerprint, string existingETag)
        {
            var link = '/' + _pathManager.GetBlobPath(fileFingerprint);

            byte[] md5Digest;

            using (var md5 = MD5.Create())
            {
                md5Digest = md5.ComputeHash(Encoding.UTF8.GetBytes(link));
            }

            var etag = S3Util.ComputeS3Etag(md5Digest);

            if (!string.IsNullOrEmpty(existingETag))
            {
                var match = string.Equals(etag, existingETag, StringComparison.CurrentCultureIgnoreCase);

                if (match)
                {
                    //Debug.WriteLine($"{collection} \"{relativePath}\" already exists and equals {link}");

                    return null;
                }
            }

            var md5DigestString = Convert.ToBase64String(md5Digest);

            var treeKey = _pathManager.GetTreeNamePath(collection, relativePath);

            var request = new PutObjectRequest
            {
                BucketName = _pathManager.Bucket,
                ContentBody = link,
                Key = treeKey,
                MD5Digest = md5DigestString,
                WebsiteRedirectLocation = link,
                ContentType = MimeDetector.Default.GetMimeType(fileFingerprint.FullFilePath),
                StorageClass = _s3StorageClass,
                Headers =
                {
                    ["x-amz-meta-lastModified"] = fileFingerprint.LastModifiedUtc.ToString("O")
                }
            };

            return new CreateLinkRequest
            {
                Collection = collection,
                RelativePath = relativePath,
                BlobLink = link,
                FileFingerprint = fileFingerprint,
                Request = request,
                ETag = etag
            };
        }
        Task SetFileFingerprintAsync(IFileFingerprint fileFingerprint)
        {
            var pathFingerprint = GetPathFingerprint(fileFingerprint.FullFilePath);

            AnnotatedPath[] annotatedPaths;

            lock (pathFingerprint)
            {
                if (null != pathFingerprint.FileFingerprint)
                    throw new InvalidOperationException("Duplicate file fingerprint for " + fileFingerprint.FullFilePath);

                pathFingerprint.FileFingerprint = fileFingerprint;

                if (0 == pathFingerprint.AnnotatedPaths.Count)
                    return Task.CompletedTask;

                annotatedPaths = pathFingerprint.AnnotatedPaths.Values.ToArray();
            }

            var tasks = annotatedPaths.Select(ap => _targetBlock.SendAsync(Tuple.Create(ap, fileFingerprint)));

            return Task.WhenAll(tasks);
        }
Beispiel #3
0
 public S3Links.ICreateLinkRequest BuildLinkRequest(string collection, string relativePath, IFileFingerprint fileFingerprint, string existingETag = null)
 {
     return _s3Links.BuildCreateLinkRequest(collection, relativePath, fileFingerprint, existingETag);
 }
Beispiel #4
0
 public string GetBlobPath(IFileFingerprint fileFingerprint)
 {
     return BlobPrefix + fileFingerprint.Fingerprint.Key();
 }
        void WriteFileFingerprint(JsonWriter writer, IFileFingerprint blob)
        {
            if (writer.WriteState != WriteState.Start)
                throw new JsonWriterException("State not closed");

            writer.WriteStartObject();

            writer.WritePropertyName("path");
            writer.WriteValue(blob.FullFilePath);
            writer.WritePropertyName("modified");
            writer.WriteValue(blob.LastModifiedUtc.ToBinary());

            var fingerprint = blob.Fingerprint;
            writer.WritePropertyName("size");
            writer.WriteValue(fingerprint.Size);
            writer.WritePropertyName("sha256");
            writer.WriteValue(fingerprint.Sha2_256);
            writer.WritePropertyName("md5");
            writer.WriteValue(fingerprint.Md5);
            writer.WritePropertyName("sha3_512");
            writer.WriteValue(fingerprint.Sha3_512);

            writer.WriteEndObject();
        }