예제 #1
0
        /// <summary>
        /// Get a hash from a file path
        /// </summary>
        private Hash GetHashFromPath(string FilePath)
        {
            string[] fileSplit = Path.GetFileNameWithoutExtension(FilePath).Split(fileDelimiter);

            DateTimeOffset?itemDate = null;

            if (fileSplit.Length == 2)
            {
                long ticks;
                if (long.TryParse(fileSplit[1], out ticks))
                {
                    itemDate = new DateTimeOffset(ticks, DateTimeOffset.Now.Offset);
                }
            }

            var hashBytes = Utility.DecodeBytes(fileSplit[0], false);

            if (hashBytes == null || hashBytes.Length != _provider.GetProviderByteLength())
            {
                return(null);
            }

            if (File.Exists(FilePath))
            {
                var fi = new FileInfo(FilePath);
                return(Hash.FromComputedBytes(hashBytes, _provider, fi.Length, itemDate));
            }
            else
            {
                return(Hash.FromComputedBytes(hashBytes, _provider, null, itemDate));
            }
        }
예제 #2
0
        public object Get([FromRoute] string hash, [FromRoute] string type)
        {
            if (hash == null)
            {
                throw new FileNotFoundException();
            }

            var parsedBytes = Utility.DecodeBytes(hash, false);

            if (parsedBytes == null)
            {
                throw new ArgumentException("Hash B64 can't be parsed");
            }

            if (parsedBytes.Length != provider.GetProviderByteLength())
            {
                throw new ArgumentException($"Hash is a invalid length, {provider} should be '{provider.GetProviderByteLength()}'");
            }

            var parsedHash = Hash.FromComputedBytes(parsedBytes, provider, null, null);

            if (parsedHash == null)
            {
                throw new ArgumentException("Hash bytes are invalid");
            }
            else
            {
                bool asBinary = false;

                switch (type)
                {
                case "html":
                    type = "text/html";
                    break;

                case "css":
                    type = "text/css";
                    break;

                case "js":
                    type = "application/javascript";
                    break;

                case "json":
                    type = "application/json";
                    break;

                case "text":
                    type = "text/plain";
                    break;

                case "jpeg":
                case "jpg":
                    type     = "image/jpeg";
                    asBinary = true;
                    break;

                case "png":
                    type     = "image/png";
                    asBinary = true;
                    break;

                case "gif":
                    type     = "image/gif";
                    asBinary = true;
                    break;

                case null:
                case "binary":
                    type     = "application/octet-stream";
                    asBinary = true;
                    break;

                default:
                    type = null;
                    break;
                }

                var item = memStore.GetItem <HashableBytes>(parsedHash);
                if (item == null)
                {
                    throw new FileNotFoundException();
                }
                else
                {
                    Response.StatusCode  = 200;
                    Response.ContentType = type;
                }

                if (asBinary)
                {
                    return(item.Value);
                }
                else
                {
                    var encoding = new ASCIIEncoding();
                    return(encoding.GetString(item.Value));
                }
            }
        }