Ejemplo n.º 1
0
        /// <summary>
        /// Gets the hash and filesize from a file that contains data about a file we need to use for updating
        /// </summary>
        /// <param name="fileStream">Stream of that file</param>
        /// <returns>SHA256 hash and filesize that is expected</returns>
        public static async Task <(string?sha256Hash, long filesize)> GetShasumDetails(this Stream fileStream)
        {
            //Grab the text from the file
            var textStream = new StreamReader(fileStream);
            var text       = await textStream.ReadToEndAsync();

            textStream.Dispose();

            //Return nothing if we don't have anything
            if (string.IsNullOrWhiteSpace(text))
            {
                return(null, -1);
            }

            //Grab what we need, checking that it's what we expect
            var textS = text.Split(' ');
            var hash  = textS[0];

            if (textS.Length != 2 ||
                string.IsNullOrWhiteSpace(hash) ||
                !SHA256Util.IsValidSHA256(hash) ||
                !long.TryParse(textS[1], out var filesize))
            {
                return(null, -1);
            }

            return(hash, filesize);
        }
Ejemplo n.º 2
0
        public ReleaseEntry(
            string sha256,
            string filename,
            long filesize,
            bool isDelta,
            SemanticVersion version,
            string folderPath,
            SemanticVersion?oldVersion = null,
            object?tag            = null,
            int?stagingPercentage = null)
        {
            //If it's a delta file then we should also be given an oldVersion
            if (isDelta)
            {
                if (oldVersion == null)
                {
                    throw new OldVersionRequiredException();
                }

                OldVersion = oldVersion;
            }

            if (filesize < 0)
            {
                throw new BadFilesizeException();
            }

            //Check hash and file name/path
            if (!SHA256Util.IsValidSHA256(sha256))
            {
                throw new InvalidHashException();
            }

            if (!filename.IsValidForFileName(out var invalidChar))
            {
                throw new InvalidFileNameException(invalidChar);
            }
            if (!folderPath.IsValidForFilePath(out var invalidPathChar))
            {
                throw new InvalidFilePathException(invalidPathChar);
            }

            _logger = LoggingCreator.CreateLogger($"{nameof(ReleaseEntry)} ({filename})");

            SHA256            = sha256;
            Filename          = filename;
            Filesize          = filesize;
            IsDelta           = isDelta;
            Version           = version;
            FileLocation      = Path.Combine(folderPath, Filename);
            StagingPercentage = stagingPercentage;
            Tag = tag;
        }