public async Task <IHttpActionResult> PostImageToken(string tokenType)
        {
            var userId = User.Identity.GetUserId();
            var user   = db.Users.Find(userId);
            var token  = Helpers.FileServerTokenManager.GenerateRandomToken();
            var hash   = Helpers.FileServerTokenManager.GenerateHash(token);

            var fst = new FileServerToken
            {
                TokenHash       = hash,
                TokenExpDateUtc = DateTime.UtcNow.Add(Helpers.FileServerTokenManager.TokenTimeSpan),
                AccountNumber   = userId
            };

            fst.FileTokenType = (FileServerTokenType)Enum.Parse(typeof(FileServerTokenType), tokenType);


            db.FileServerTokens.Add(fst);
            await db.SaveChangesAsync();

            return(Created <object>("DefaultApi", new { userId = userId, token = System.Web.HttpUtility.UrlEncode(token) }));
        }
Exemplo n.º 2
0
        public static async Task <UploaderResponse> UploadBase64ImageAsync(
            ApplicationDbContext ctx, string userId, string base64Image, FileServerTokenType tokenType, bool encrypt = false)
        {
            byte[] imgData = Convert.FromBase64String(
                base64Image.Substring(base64Image.IndexOf("base64,") + 7));

            if (imgData.Length > SizeLimits[tokenType])
            {
                return(new UploaderResponse(HttpStatusCode.BadRequest, "Large Image:" + imgData.Length.ToString(), "", 0));
            }


            var token = FileServerTokenManager.GenerateRandomToken();
            var hash  = FileServerTokenManager.GenerateHash(token);

            var user = ctx.Users.Find(int.Parse(userId));

            if (user == null)
            {
                throw new Exception("Invalid userId");
            }

            var fst = new FileServerToken
            {
                TokenHash       = hash,
                TokenExpDateUtc = DateTime.UtcNow.Add(FileServerTokenManager.TokenTimeSpan),
                AccountNumber   = userId,
                FileTokenType   = tokenType
            };

            ctx.FileServerTokens.Add(fst);
            await ctx.SaveChangesAsync();

            var values = new System.Collections.Specialized.NameValueCollection();

            values.Add("accountNumber", fst.AccountNumber);
            values.Add("token", System.Web.HttpUtility.UrlEncode(token));
            int?bf_idx = null;

            if (encrypt)
            {
                bf_idx  = EncryptionService.NewBF_Idx();
                imgData = EncryptionService.EncryptBytes(imgData, (int)bf_idx);
            }
            var files = new[]
            {
                new UploadFile
                {
                    Name      = "file",
                    Filename  = "myfile.jpg",
                    DataBytes = imgData
                }
            };

            var    fileServerId = 1; //TODO FUTURE: Replace by file server selection logic
            string filePath     = "";
            //TODO: What if upload fails?
            string         responseText = "";
            string         debug_log    = "";
            HttpStatusCode status;

            using (var uploaderResponse = await UploadFilesAsync(
                       "http://" + ctx.FileServers.First().ServerIP + "/uploader.php", files, values))
            {
                HttpWebResponse httpResponse = (HttpWebResponse)(uploaderResponse);
                using (var data = uploaderResponse.GetResponseStream())
                {
                    using (var reader = new StreamReader(data))
                    {
                        responseText = await reader.ReadToEndAsync();
                    }
                }
                status = httpResponse.StatusCode;
                if (status == HttpStatusCode.OK || status == HttpStatusCode.Created)
                {
                    filePath   = responseText;
                    debug_log += "Image Path:" + responseText + "\r\n";
                }
                else
                {
                    debug_log += "Image Upload Error Code:" + httpResponse.StatusCode + "|";
                    debug_log += responseText + "\r\n";
                }
            }
            return(new UploaderResponse(status, debug_log, filePath, fileServerId, bf_idx));
        }