示例#1
0
        /// <summary>
        /// Returns an uploaded file from the file list if it exists. If it doens't, this returns null.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private EchoUploadedFile GetFileFromList(List <EchoUploadedFile> files, string name, ArkUploadedFileType type)
        {
            var results = files.Where(x => x.type == type && x.name == name);

            return(results.FirstOrDefault());
        }
示例#2
0
        /// <summary>
        /// Updates a file on the server. If the file changed, this returns true.
        /// </summary>
        /// <param name="files"></param>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private async Task <bool> UpdateFileAsync(List <EchoUploadedFile> files, string filename, string name, ArkUploadedFileType type)
        {
            //Get the data if it exists
            EchoUploadedFile fileData = GetFileFromList(files, name, type);

            //Open a stream on this file
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            //Hash this file
            string hash;

            using (SHA1Managed sha1 = new SHA1Managed())
            {
                byte[] hashBytes = sha1.ComputeHash(fs);
                hash = string.Concat(hashBytes.Select(b => b.ToString("x2")));
            }

            //Check if this file needs updating
            if (fileData != null)
            {
                if (fileData.sha1 == hash)
                {
                    return(false);
                }
            }

            //This file needs updating. Compress and upload this file.
            fs.Position = 0;
            Log.I("Update-File", $"Updating {name}/{type.ToString()} ({MathF.Round(fs.Length / 1024)} KB) for server {info.id}...");
            HttpResponseMessage response;

            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream gz = new GZipStream(ms, CompressionLevel.Optimal, true))
                {
                    //Compress
                    await fs.CopyToAsync(gz);
                }

                //Upload
                ms.Position = 0;
                StreamContent sc = new StreamContent(ms);
                sc.Headers.Add("X-Delta-Filename", name);
                sc.Headers.Add("X-Delta-File-Type", type.ToString());
                try
                {
                    response = await client.PostAsync(Program.config.endpoint_echo_upload, sc);
                }
                catch
                {
                    throw new StandardError("Update-File", $"Failed to upload updated file. {name}/{type.ToString()} ({MathF.Round(fs.Length / 1024)} KB)");
                }
            }


            if (!response.IsSuccessStatusCode)
            {
                throw new StandardError("Update-File", $"Failed to upload updated file. Status code was not OK. {name}/{type.ToString()} ({MathF.Round(fs.Length / 1024)} KB)");
            }

            return(true);
        }