Example #1
0
        public FileDescription UploadSingleFile(IFormFile iFormFile, FileType fileType, string entityName, DateTime dateTime, string description = null)
        {
            FileDescription fileDescription = new FileDescription
            {
                Enabled   = true,
                Ext       = GetExtension(iFormFile.FileName),
                DateAdded = DateTime.Now,
                FileType  = fileType,
                Id        = Guid.NewGuid(),
                //ContentType=iFormFile.ContentType,
                FileName    = entityName + "_" + fileType.ToString() + "_" + dateTime.ToString("yyyy-MM-dd"),
                Description = description
                              //dodać kodowanie procentowe znaków nazwy pliku
            };
            BackblazeAPI api = new BackblazeAPI();

            api.AutorizeAccount();
            BakcblazeGetUploadUrlResponse urlResponse = api.GetUploadUrl();

            MemoryStream ms = new MemoryStream();

            iFormFile.CopyTo(ms);
            ms.Close();
            byte[] bytes = ms.ToArray();

            string backblazeFileId = api.UploadFile(fileDescription.FileName, bytes, iFormFile.ContentType, urlResponse);

            fileDescription.BackblazeFileId = backblazeFileId;
            return(fileDescription);
        }
Example #2
0
        public BakcblazeGetUploadUrlResponse GetUploadUrl()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ApiUrl + "/b2api/v2/b2_get_upload_url");
            string         body       = "{\"bucketId\":\"" + bucketId + "\"}";
            var            data       = Encoding.UTF8.GetBytes(body);

            webRequest.Method = "POST";
            webRequest.Headers.Add("Authorization", AutorizationToken);
            webRequest.ContentType   = "application/json; charset=utf-8";
            webRequest.ContentLength = data.Length;
            using (var stream = webRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
                stream.Close();
            }
            WebResponse response       = (HttpWebResponse)webRequest.GetResponse();
            var         responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            response.Close();
            BakcblazeGetUploadUrlResponse urlResponse = JsonConvert.DeserializeObject <BakcblazeGetUploadUrlResponse>(responseString);

            return(urlResponse);
        }
Example #3
0
        public string UploadFile(string fileName, byte[] file, string contentType, BakcblazeGetUploadUrlResponse urlResponse)
        {
            string sha1Str;
            SHA1   sha1 = SHA1.Create();

            // NOTE: Loss of precision. You may need to change this code if the file size is larger than 32-bits.
            byte[]        hashData = sha1.ComputeHash(file, 0, file.Length);
            StringBuilder sb       = new StringBuilder();

            foreach (byte b in hashData)
            {
                sb.Append(b.ToString("x2"));
            }
            sha1Str = sb.ToString();

            // Send over the wire
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(urlResponse.UploadUrl);

            webRequest.Method = "POST";
            webRequest.Headers.Add("Authorization", urlResponse.AuthorizationToken);
            webRequest.Headers.Add("X-Bz-File-Name", b2UrlDecode(fileName));
            webRequest.Headers.Add("X-Bz-Content-Sha1", sha1Str);
            webRequest.ContentType = contentType;
            using (var stream = webRequest.GetRequestStream())
            {
                stream.Write(file, 0, file.Length);
                stream.Close();
            }
            WebResponse response       = (HttpWebResponse)webRequest.GetResponse();
            var         responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

            response.Close();
            JObject obj    = JObject.Parse(responseString);
            string  fileId = (string)obj["fileId"];

            return(fileId);
        }