public ActionResult GetDocument(int P_ARBRFART_ID, int P_CDIDIOMA, string P_IMPRES_DETALHE) { try { if (P_ARBRFART_ID == null || P_CDIDIOMA == null || string.IsNullOrEmpty(P_IMPRES_DETALHE)) { return(null); } var manager = new RequestManager(); string hidden_run_parameters = "hidden_run_parameters=server=rep_bnsvher280_rep10g&report=%2Fhome%2Freport%2Foracle10g%2Freports%2Fhome%2Fcoml%2Fsist%2Far0148.rdf&destype=cache&desformat=pdf&userid=COML%2FCOML%40CORP1_qa&P_ARBRFART_ID={0}&PARAMFORM=YES&P_INDPERCCOMPOS=S&P_IMPRES_INSUMO=S&P_IMPRES_IMAGEM=S&P_IMPRES_MEDIDA=S&P_CDIDIOMA={1}&P_IMPRES_DETALHE={2}"; string content = string.Format(hidden_run_parameters, P_ARBRFART_ID, P_CDIDIOMA, P_IMPRES_DETALHE); string uri = "http://bnsvher280.hering.local:7777/reports/rwservlet?"; var dados = manager.GetResponseContent(manager.SendPOSTRequest(uri, content, null, null, true)); ViewBag.AmazonUrl = AmazonS3Helper.CreateFileShare(P_ARBRFART_ID.ToString(), dados); return(View()); } catch (Exception ex) { ViewBag.Error = ex.ToString(); return(View()); } }
public IActionResult DeleteFile(string websiteid, [FromQuery] string assetFileName) { if (string.IsNullOrEmpty(websiteid)) { return(BadRequest("Website id can not be empty")); } if (string.IsNullOrEmpty(assetFileName)) { return(BadRequest("File name can not be empty")); } var userId = AuthHelper.AuthorizeRequest(Request); if (string.IsNullOrEmpty(userId)) { return(new CommonActionResult(CommonAPIResponse.UnAuthorized())); } //TODO : Validate the userid with websiteid var bucketName = "kitsune-website-files-cdn"; assetFileName = assetFileName.Trim(); var result = AmazonS3Helper.DeleteAsset($"v1/{websiteid}/{assetFileName}", bucketName); if (result) { return(Ok()); } return(BadRequest("Invalid website id")); }
private static string MoveToS3(AmazonS3Client s3Client, string bucket, int?chunkId, int?subChunkId, IDataReader reader, string tableName) { var folder = tableName; string fileName; if (chunkId.HasValue) { var indexOfSet = ServicesManager.GetIndexOfSet(chunkId.Value); fileName = string.Format(@"{0}/{1}", indexOfSet, tableName + ".txt.gz." + chunkId + "." + subChunkId); } else { fileName = string.Format(@"{0}", folder + ".txt.gz"); } if (!(AmazonS3Util.DoesS3BucketExist(s3Client, Settings.Current.Bucket))) { AmazonS3Helper.CreateBucket(s3Client, Settings.Current.Bucket); } AmazonS3Helper.CopyFile(s3Client, bucket, fileName, reader, tableName); return(fileName); }
public void ClenupBucket() { var folder = string.Format("{0}/{1}", Settings.Current.Building.Vendor, Settings.Current.Building.Id); AmazonS3Helper.ClenupBucket(currentClient, folder); }
public static bool GetAndSaveRobotsTxt(string projectId, Uri uri) { try { WebClient webClient = new WebClient(); webClient.Encoding = System.Text.Encoding.Default; Byte[] robots = webClient.DownloadData(uri.AbsoluteUri + "robots.txt"); if (robots != null) { return(AmazonS3Helper.SaveTheFileInS3(EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSAccessKey, EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSSecretKey, projectId + uri.LocalPath + "robots.txt", robots, EnvironmentConstants.ApplicationConfiguration.AWSBuckets.SourceBucket.Name)); } return(false); } catch (Exception ex) { Log.Error(ex, $"ProjectId:{projectId}, Message:Error while searching Robots.txt for Url : {uri.AbsoluteUri}"); try { Byte[] robotsValue = Encoding.ASCII.GetBytes("User-agent: * /n allow: /"); return(AmazonS3Helper.SaveTheFileInS3(EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSAccessKey, EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSSecretKey, projectId + uri.LocalPath + "robots.txt", robotsValue, EnvironmentConstants.ApplicationConfiguration.AWSBuckets.SourceBucket.Name)); } catch (Exception innerEx) { Log.Error(innerEx, $"ProjectId:{projectId}, Message:Error while created a default Robots.txt for Url : {uri.AbsoluteUri}"); } return(false); } }
public static bool SaveInS3AndUpdateDB(string projectId, AssetDetails asset, FileType fileType, Byte[] bytes, string contentType) { try { //update the details var dbResult = MongoHelper.UpdateDownloadDetailsAsync(projectId, asset, fileType); //save the file if (bytes != null) { AmazonS3Helper.SaveTheFileInS3(EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSAccessKey, EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSSecretKey, String.Format("{0}{1}", projectId, asset.NewUrl), bytes, EnvironmentConstants.ApplicationConfiguration.AWSBuckets.SourceBucket.Name, contentType); } return(dbResult.Result); } catch (Exception ex) { Log.Error(ex, $"Error saving in s3 for projectId : {projectId} and file : {asset.LinkUrl}"); return(false); } }
private void CleanAccounts(string azureaccountname, string azureaccountkey, string amazons3accountname, string amazons3accountkey) { AzureHelper helper = new AzureHelper(azureaccountname, azureaccountkey, "foo", CompressionType.None, EncryptionType.None, null, null, new Logger(), 0, 0); helper.CleanAccount(); AmazonS3Helper s3helper = new AmazonS3Helper(new RemoteInfo(amazons3accountname, amazons3accountkey), "foo723r2y3r723rh27r8i", CompressionType.None, EncryptionType.None, null, null, new Logger(), 0, 0); s3helper.DeleteAllBuckets(); }
private void LoadPregnancyDrug(bool storeToS3) { var sql = File.ReadAllText(Path.Combine(Settings.Settings.Current.Builder.Folder, @"Core\Lookups\PregnancyDrug.sql")); sql = sql.Replace("{sc}", Settings.Settings.Current.Building.VocabularySchemaName); Console.WriteLine("PregnancyDrug - Loading..."); using (var connection = SqlConnectionHelper.OpenOdbcConnection(Settings.Settings.Current.Building.VocabularyConnectionString)) using (var command = new OdbcCommand(sql, connection) { CommandTimeout = 0 }) using (var reader = command.ExecuteReader()) { if (storeToS3) { var fileName = $"{Settings.Settings.Current.Building.Vendor}/{Settings.Settings.Current.Building.Id}/Lookups/PregnancyDrug.txt.gz"; Console.WriteLine("PregnancyDrug - store to S3 | " + fileName); using (var client = new AmazonS3Client( Settings.Settings.Current.S3AwsAccessKeyId, Settings.Settings.Current.S3AwsSecretAccessKey, new AmazonS3Config { Timeout = TimeSpan.FromMinutes(60), RegionEndpoint = Amazon.RegionEndpoint.USEast1, MaxErrorRetry = 20, })) { AmazonS3Helper.CopyFile(client, Settings.Settings.Current.Bucket, fileName, reader); } } else { Console.WriteLine("PregnancyDrug - filling"); var lookup = new Lookup(); while (reader.Read()) { lookup.Add(new LookupValue { ConceptId = int.Parse(reader[0].ToString()), SourceCode = reader[0].ToString() }); } _lookups.Add("PregnancyDrug", lookup); } } Console.WriteLine("PregnancyDrug - Done"); }
public static string UploadMessageAttachment(HttpPostedFileBase file, string contentType, string FileName) { string Url = ""; if (AmazonS3Helper.WritingAnObject(file.InputStream, contentType, FileName)) { Url = "https://s3.eu-central-1.amazonaws.com/alrajhisolutions/PMSystem/" + FileName; } return(Url); }
public async Task <IActionResult> DownloadTimeline(RequestTimelinePayload payload) { using (var amazonClient = new AmazonS3Helper(_amazonS3)) { var timeline = await GetTimeLine(_context, payload.Id, payload.ListIds); if (timeline == null) { return(NotFound()); } return(Json(new { url = await amazonClient.PrepareZip(timeline) })); } }
public void AmazonS3ChunkWriteRead() { string accountName = ""; string accountKey = ""; RemoteInfo ri = new RemoteInfo(accountName, accountKey); AmazonS3Helper helper = new AmazonS3Helper(new RemoteInfo(accountName, accountKey), "testupl0", CompressionType.GZip, EncryptionType.None, null, null, new Logger(), 4 * 1024 * 1024, 1); helper.UploadFileAsChunks("D:\\testfiles\\test.txt"); int OFFSET_TO_READ = 4492321; int BYTES_TO_READ = 11000; List <ChunkInfo> metadata = helper.GetObjectMetadata("test.txt").Item1; Dictionary <int, long> chunkindexandoffsets = helper.GetChunkIndexAndOffsetInChunk(metadata, OFFSET_TO_READ, BYTES_TO_READ); byte[] temp = null; foreach (int chunkIndex in chunkindexandoffsets.Keys) { if (temp != null) { temp = temp.Concat(helper.DownloadChunk("test.txt", chunkIndex)).ToArray(); } else { temp = helper.DownloadChunk("test.txt", chunkIndex); } } byte[] test = temp.Skip((int)chunkindexandoffsets.ElementAt(0).Value).Take(BYTES_TO_READ).ToArray(); byte[] truth = new byte[BYTES_TO_READ]; using (BinaryReader reader = new BinaryReader(new FileStream("D:\\testfiles\\test.txt", FileMode.Open))) { reader.BaseStream.Seek(OFFSET_TO_READ, SeekOrigin.Begin); reader.Read(truth, 0, BYTES_TO_READ); } bool arraysAreEqual = Enumerable.SequenceEqual(test, truth); Console.WriteLine(arraysAreEqual); if (!arraysAreEqual) { throw new Exception("local and downloaded bits dont match"); } }
public static bool SaveInS3(string projectId, string filePath, string htmlString) { try { var bytes = Encoding.Default.GetBytes(htmlString); filePath = filePath.TrimStart('/'); return(AmazonS3Helper.SaveTheFileInS3(EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSAccessKey, EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSSecretKey , filePath, bytes, EnvironmentConstants.ApplicationConfiguration.AWSBuckets.SourceBucket.Name)); } catch (Exception ex) { Log.Error(ex, $"Error saving the file : {filePath} for projectId : {projectId}"); return(false); } }
public IHttpActionResult putObject(String provider) { try { string jsonPost = Request.Content.ReadAsStringAsync().Result; Dictionary <string, object> jsonPostDict = Durados.Web.Mvc.UI.Json.JsonSerializer.Deserialize(jsonPost); if (!jsonPostDict.ContainsKey("key")) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Conflict, "You must send the key parameter"))); } String key = (String)jsonPostDict["key"]; if (!jsonPostDict.ContainsKey("secret")) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Conflict, "You must send the secret parameter"))); } String secret = (String)jsonPostDict["secret"]; if (!jsonPostDict.ContainsKey("bucket")) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Conflict, "You must send the bucket parameter"))); } String bucket = (String)jsonPostDict["bucket"]; //String region = (String)jsonPostDict["region"]; if (!jsonPostDict.ContainsKey("filename")) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Conflict, "You must send the filename parameter"))); } String filename = System.Web.HttpContext.Current.Server.UrlDecode((String)jsonPostDict["filename"]); if (!jsonPostDict.ContainsKey("filedata")) { return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Conflict, "You must send the filedata parameter"))); } String filedata = (String)jsonPostDict["filedata"]; byte[] bytes = Convert.FromBase64String(filedata); string url = string.Empty; using (var dataStream = new MemoryStream(bytes)) { url = AmazonS3Helper.SaveUploadedFileToAws(key, secret, bucket, filename.Replace(" ", "-"), null, dataStream); } return(Ok(new { url = url })); } catch (Exception exception) { throw new BackAndApiUnexpectedResponseException(exception, this); } }
public async Task <IActionResult> GetUserInfo() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(Unauthorized()); } var userDetail = await _context.Users.FirstOrDefaultAsync(x => x.Id == user.Id); return(Json(new { userDetail?.Name, userDetail?.Surname, Email = user.Email.ToLowerInvariant(), user.DateOfAccident, ProfilePicture = AmazonS3Helper.GenerateUrl(user.ProfilePictureKey), IsAssigned = user.AssignedToAttorneyId.HasValue })); }
private static string SaveToS3CSV(AmazonS3Client s3Client, string bucket, int?chunkId, int?subChunkId, IDataReader reader, string tableName) { var folder = tableName; string fileName; if (chunkId.HasValue) { fileName = $@"{chunkId}/{tableName + ".txt.gz." + chunkId + "." + subChunkId}"; } else { fileName = $@"{folder + ".txt.gz"}"; } AmazonS3Helper.CopyFile(s3Client, bucket, fileName, reader); return(fileName); }
public IActionResult UploadFiles(string websiteId, [FromQuery] string assetFileName, IFormFile file) { Stream stream = Request.Body; var byteArrayStream = Kitsune.API2.EnvConstants.Constants.ReadInputStream(file); if (string.IsNullOrEmpty(websiteId)) { return(BadRequest("WebsiteId cannot be empty")); } if (string.IsNullOrEmpty(assetFileName)) { return(BadRequest("File name cannot be empty")); } if (byteArrayStream == null || byteArrayStream.Length == 0) { return(BadRequest("File cannot be empty")); } websiteId = websiteId.Replace(" ", ""); var userid = Request.Headers.ContainsKey("Authorization") ? Request.Headers["Authorization"].ToString() : null; var projectId = MongoConnector.GetProjectIdFromWebsiteId(websiteId); var bucketName = "kitsune-resource-production"; var temporaryFileName = Helper.KitsuneCommonUtils.FormatResourceFileName(assetFileName)?.ToLower(); var result = AmazonS3Helper.SaveAssetsAndReturnObjectkey($"{projectId}/websiteresources/{websiteId}/{temporaryFileName}", byteArrayStream, bucketName); if (!string.IsNullOrEmpty(result)) { return(Ok("Uploaded successfully")); } // do something witht his stream now return(BadRequest("Invalid website id")); }
public async Task <IActionResult> Index(int page = 0, int itemPerPage = 10) { Predicate <ApplicationUser> conditionalResults = x => true; var user = await _userManager.GetUserAsync(User); if (User.IsInRole("Attorney")) { var userId = new Guid(user.Id); var attorney = await _context.Attorneys.FirstOrDefaultAsync(x => x.AttorneyUserId == userId); conditionalResults = x => x.AssignedToAttorneyId == attorney.Id; } var total = await _context.Users.CountAsync(); var list = await _context.Users .Include(x => x.AssignedToAttorney) .Where(x => conditionalResults(x)) .Select(x => new CreateClientViewModel { //RegistrationCode = x.Code, Surname = x.Surname, Name = x.Name, Email = x.UserName, Id = x.Id, CreatedAt = x.DateOfAccident, AssignedToAttorney = x.AssignedToAttorneyId.HasValue ? (x.AssignedToAttorney.Name + " " + x.AssignedToAttorney.Surname) : "Not Assigned", ImageURL = AmazonS3Helper.GenerateUrl(x.ProfilePictureKey, "attorney-journal-dev") }) .OrderByDescending(x => x.CreatedAt) // .Skip (page * itemPerPage) // .Take (itemPerPage) .ToListAsync(); return(View(new TablePagination <CreateClientViewModel> { Total = total, Lists = list })); }
public async Task <ActionResult> Put(FIleUploadAPI UplodedFile) { var uuid = Guid.Parse(User.Identity.Name); User userFromTokenId = await _context.Users.FirstOrDefaultAsync(u => u.Id == uuid); if (userFromTokenId == null) { return(Unauthorized()); } if (UplodedFile.files == null) { return(BadRequest("No files found".ToBadRequest())); } var type = UplodedFile.files.ContentType; if (type != "image/jpeg" && type != "image/png" && type != "application/x-jpg") { return(BadRequest("Only image supported".ToBadRequest())); } try { string name = await AmazonS3Helper.SaveImageToBucket(UplodedFile.files); if (string.IsNullOrEmpty(name)) { return(BadRequest("Save failed".ToBadRequest())); } userFromTokenId.SetAvatar(name); _context.Update(userFromTokenId); await _context.SaveChangesAsync(); _log.Information("User {Email} has changed his/her avatar_url on {date} ", userFromTokenId.Email, DateTime.Now); return(Created("img", Format.ToMessage(userFromTokenId.ToMessage(), 201))); } catch (Exception e) { _log.Fatal(e.Message + "on Put User on {Date}", DateTime.Now); return(StatusCode(500)); } }
/// <summary> /// Get the FaviconUrl from the given Uri /// returns null if not present /// </summary> /// <param name="projectId"></param> /// <param name="uri"></param> /// <returns></returns> public static string GetFaviconUrl(string projectId, Uri uri) { try { #region Get the favicon Icon Url Uri link = new Uri(uri.AbsoluteUri); WebClient webClient = new WebClient(); webClient.Encoding = System.Text.Encoding.Default; var originalHtml = webClient.DownloadString(uri); KHtmlDocument doc = new KHtmlDocument(); doc.LoadHtml(originalHtml); var faviconUri = doc.GetFaviconIcon(uri); #endregion #region Download the Favicon Icon webClient.Encoding = System.Text.Encoding.Default; Byte[] favicon = webClient.DownloadData(faviconUri); AmazonS3Helper.SaveTheFileInS3(EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSAccessKey, EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSSecretKey, projectId + faviconUri.LocalPath, favicon, EnvironmentConstants.ApplicationConfiguration.AWSBuckets.SourceBucket.Name); #endregion return($"/{projectId}{faviconUri.LocalPath}"); } catch (Exception ex) { Log.Error(ex, $"ProjectId:{projectId}, Message:Error while searching favicon icon for Url : {uri.AbsoluteUri}"); //EventLogger.Write(ex, String.Format("Error while searching favicon icon for Url : {0}", uri.AbsoluteUri), projectId); return(null); } }
public IActionResult UploadFiles(string websiteid, [FromQuery] string assetFileName, IFormFile file) { Stream stream = Request.Body; var byteArrayStream = Kitsune.API2.EnvConstants.Constants.ReadInputStream(file); if (string.IsNullOrEmpty(websiteid)) { return(BadRequest("Website id can not be empty")); } if (string.IsNullOrEmpty(assetFileName)) { return(BadRequest("File name can not be empty")); } if (byteArrayStream == null || byteArrayStream.Length == 0) { return(BadRequest("File can not be empty")); } websiteid = websiteid.Replace(" ", ""); //TODO : Validate the websiteid against userid var userId = AuthHelper.AuthorizeRequest(Request); if (string.IsNullOrEmpty(userId)) { return(new CommonActionResult(CommonAPIResponse.UnAuthorized())); } else { var website = MongoConnector.GetKitsuneWebsiteDetails(websiteid); if (website != null && website.Response != null) { if (userId != ((WebsiteDetailsResponseModel)website.Response).DeveloperId) { return(new CommonActionResult(CommonAPIResponse.UnAuthorized())); } } else { return(new CommonActionResult(CommonAPIResponse.BadRequest(new System.ComponentModel.DataAnnotations.ValidationResult("Website id invalid")))); } } var bucketName = "kitsune-website-files-cdn"; var tempName = Kitsune.Helper.KitsuneCommonUtils.FormatResourceFileName(assetFileName).ToLower(); var lastIndexOfDot = tempName.LastIndexOf('.'); var fileName = string.Format("{0}-{1}{2}", lastIndexOfDot == -1 ? tempName : tempName.Substring(0, lastIndexOfDot), ObjectId.GenerateNewId().ToString(), lastIndexOfDot == -1 ? tempName : tempName.Substring(lastIndexOfDot)); var result = AmazonS3Helper.SaveAssetsAndReturnObjectkey($"v1/{websiteid}/{fileName}", byteArrayStream, bucketName); if (!string.IsNullOrEmpty(result)) { var domainUrl = EnvConstants.Constants.KitsuneFilesCDNUrl; return(Ok(String.Format("{0}/{1}", domainUrl, result))); } // do something witht his stream now return(BadRequest("Invalid website id")); }
private void Load(IEnumerable <EntityDefinition> definitions, bool storeToS3) { if (definitions == null) { return; } foreach (var ed in definitions) { ed.Vocabulary = this; if (ed.Concepts == null) { continue; } foreach (var c in ed.Concepts) { if (c.ConceptIdMappers == null) { continue; } foreach (var conceptIdMapper in c.ConceptIdMappers) { if (!string.IsNullOrEmpty(conceptIdMapper.Lookup)) { if (!_lookups.ContainsKey(conceptIdMapper.Lookup)) { string sql = string.Empty; var vendorFolder = Settings.Settings.Current.Building.Vendor.GetAttribute <FolderAttribute>().Value; var sqlFileDestination = Path.Combine(Settings.Settings.Current.Builder.Folder, "Core", "Transformation", vendorFolder, "Lookups", conceptIdMapper.Lookup + ".sql"); var baseSql = File.ReadAllText(Path.Combine(Settings.Settings.Current.Builder.Folder, @"Core\Lookups\Base.sql")); sql = File.ReadAllText(sqlFileDestination); sql = sql.Replace("{base}", baseSql); sql = sql.Replace("{sc}", Settings.Settings.Current.Building.VocabularySchemaName); try { Console.WriteLine(conceptIdMapper.Lookup + " - Loading..."); using (var connection = SqlConnectionHelper.OpenOdbcConnection(Settings.Settings.Current.Building.VocabularyConnectionString)) using (var command = new OdbcCommand(sql, connection) { CommandTimeout = 0 }) using (var reader = command.ExecuteReader()) { if (storeToS3) { var fileName = $"{Settings.Settings.Current.Building.Vendor}/{Settings.Settings.Current.Building.Id}/Lookups/{conceptIdMapper.Lookup}.txt.gz"; Console.WriteLine(conceptIdMapper.Lookup + " - store to S3 | " + fileName); using (var client = new AmazonS3Client( Settings.Settings.Current.S3AwsAccessKeyId, Settings.Settings.Current.S3AwsSecretAccessKey, new AmazonS3Config { Timeout = TimeSpan.FromMinutes(60), RegionEndpoint = Amazon.RegionEndpoint.USEast1, MaxErrorRetry = 20, })) { AmazonS3Helper.CopyFile(client, Settings.Settings.Current.Bucket, fileName, reader); } } else { Console.WriteLine(conceptIdMapper.Lookup + " - filling"); var lookup = new Lookup(); while (reader.Read()) { var lv = CreateLookupValue(reader); lookup.Add(lv); } _lookups.Add(conceptIdMapper.Lookup, lookup); } } Console.WriteLine(conceptIdMapper.Lookup + " - Done"); } catch (Exception) { Console.WriteLine("Lookup error [file]: " + sqlFileDestination); Console.WriteLine("Lookup error [query]: " + sql); Logger.WriteWarning("Lookup error [file]: " + sqlFileDestination); Logger.WriteWarning("Lookup error [query]: " + sql); throw; } } } } } } }
public async Task <IActionResult> Upload() { //StringValues UniqueUploadId = default(StringValues); //StringValues UploadType = default(StringValues); //Request.Headers.TryGetValue("unique-upload-id", out UniqueUploadId); //Request.Headers.TryGetValue("upload-type", out UploadType); Request.Headers.TryGetValue("X-Upload-Title", out StringValues uploadTitle); Request.Headers.TryGetValue("X-Date-Taken", out StringValues dateTaken); var user = await _userManager.GetUserAsync(User); if (user == null) { return(BadRequest()); } if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { return(BadRequest($"Expected a multipart request, but got {Request.ContentType}")); } // Used to accumulate all the form url encoded key value pairs in the // request. var formAccumulator = new KeyValueAccumulator(); var objectKey = Guid.NewGuid().ToString(); var thumbObjectKey = $"{Guid.NewGuid()}.jpg"; FileStorage parent = null; var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), DefaultFormOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); while (section != null) { var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out ContentDispositionHeaderValue contentDisposition); if (hasContentDispositionHeader) { if (MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { var targetFilePath = Path.GetTempFileName(); using (var targetStream = System.IO.File.Create(targetFilePath)) { await section.Body.CopyToAsync(targetStream); _logger.LogInformation($"Copied the uploaded file '{targetFilePath}'"); } var partName = contentDisposition.Name; var strippedFileName = contentDisposition.FileName.Replace("\"", string.Empty); var mimeType = MimeTypesHelper.GetMimeType(strippedFileName); var fileExtension = $".{strippedFileName.Split('.').LastOrDefault()}"; var originalFileName = strippedFileName.Replace(fileExtension, string.Empty); var type = FileStorageType.Thumb; var isFile = partName.Replace("\"", string.Empty) == "file"; if (mimeType.Contains("image") && isFile) { type = FileStorageType.Image; } else if (mimeType.Contains("video") && isFile) { type = FileStorageType.Video; } if (isFile) { objectKey = $"{objectKey}{fileExtension}"; } try { using (var amazonClient = new AmazonS3Helper(_amazonS3, "attorney-journal-dev")) { await amazonClient.UploadFileAsync(targetFilePath, isFile?objectKey : thumbObjectKey); } var date = new DateTime(); try { date = DateTime.Parse(dateTaken); } catch (System.Exception) { } var newFile = new FileStorage { Parent = parent, AmazonObjectKey = isFile ? objectKey : thumbObjectKey, CreatedAt = DateTime.UtcNow, FileExtension = fileExtension, OriginalName = originalFileName, MimeType = mimeType, Owner = user, Type = type, Title = uploadTitle.FirstOrDefault() ?? string.Empty, DateTaken = date }; _context.Files.Add(newFile); parent = isFile ? newFile : null; } catch (AmazonS3Exception s3Exception) { Console.WriteLine(s3Exception.Message, s3Exception.InnerException); } } } // Drains any remaining section body that has not been consumed and // reads the headers for the next section. section = await reader.ReadNextSectionAsync(); } // Bind form data to a model var formValueProvider = new FormValueProvider(BindingSource.Form, new FormCollection(formAccumulator.GetResults()), CultureInfo.CurrentCulture); var bindingSuccessful = await TryUpdateModelAsync(new { }, string.Empty, formValueProvider); if (!bindingSuccessful || !ModelState.IsValid) { return(BadRequest(ModelState)); } await _context.SaveChangesAsync(CancellationToken.None); return(Json(new { fileUrl = AmazonS3Helper.GenerateUrl(objectKey), thumbUrl = AmazonS3Helper.GenerateUrl(thumbObjectKey), result = true })); }
public static void Replace(AssetDetails Link, List <AssetDetails> allResources, string projectId, List <FindAndReplace> findAndReplaces, bool updateDB) { try { if (Link.NewUrl == null) { return; } if (!Link.ResponseStatusCode.Equals(HttpStatusCode.OK)) { return; } string originalHtml = string.Empty; string downloadString = String.Empty; WebClient webClient = new WebClient { Encoding = System.Text.Encoding.Default }; downloadString = String.Format("{0}/{1}{2}", EnvironmentConstants.ApplicationConfiguration.AWSBuckets.SourceBucket.Url, projectId, Link.NewUrl); originalHtml = webClient.DownloadString(downloadString); var count = Link.NewUrl.Count(x => x == '/'); var charToAdd = ""; if (count == 0 || count == 1) { charToAdd = "."; } else if (count >= 2) { charToAdd = ".."; count = count - 2; while (count > 0) { charToAdd = "../" + charToAdd; count--; } } foreach (var file in allResources) { try { if (file.NewUrl == null) { originalHtml = originalHtml.Replace(file.PlaceHolder, file.LinkUrl); } else { originalHtml = originalHtml.Replace(file.PlaceHolder, charToAdd + file.NewUrl); } } catch (Exception ex) { Log.Error(ex, $"ProjectId:{projectId}, Message:Error Replacing PlaceHolder for Url: {Link.LinkUrl}"); } } #region Find and replace Regex if (findAndReplaces != null) { foreach (var regex in findAndReplaces) { try { originalHtml = Regex.Replace(originalHtml, regex.Find, regex.Replace, RegexOptions.IgnoreCase); } catch (Exception ex) { Log.Error(ex, $"ProjectId:{projectId}, Message:Error Replacing PlaceHolder for Url: {Link.LinkUrl}"); } } } #endregion var byteArray = Encoding.Default.GetBytes(originalHtml); Task task = null; if (updateDB) { task = MongoHelper.UpdateDBForPlaceHolderReplacer(projectId); } AmazonS3Helper.SaveTheFileInS3(EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSAccessKey, EnvironmentConstants.ApplicationConfiguration.AWSS3Configuration.AWSSecretKey, String.Format("{0}{1}", projectId, Link.NewUrl), byteArray, EnvironmentConstants.ApplicationConfiguration.AWSBuckets.SourceBucket.Name); if (updateDB) { task.Wait(); } } catch (Exception ex) { string url = Link == null ? "url was null" : Link.LinkUrl; Log.Error(ex, $"ProjectId:{projectId}, Message:Error Replacing PlaceHolder for Url: {url}"); } }
public static async Task <UserTimelineModel> GetTimeLine(ApplicationDbContext context, string userId, List <Guid> listIds = null, bool showDummy = false) { var userDetail = await context.Users.FirstOrDefaultAsync(x => x.Id == userId); if (userDetail == null) { return(null); } var userTimeline = new UserTimelineModel(); IQueryable <FileStorage> files = context.Files; if (listIds != null && listIds.Any()) { files = context.Files.Where(x => listIds.Contains(x.Id)); } userTimeline.Name = userDetail.Name; userTimeline.Surname = userDetail.Surname; userTimeline.UserId = userDetail.Id; userTimeline.DateOfAccident = userDetail.DateOfAccident.GetValueOrDefault(DateTime.UtcNow); userTimeline.PhotoURL = AmazonS3Helper.GenerateUrl(userDetail.ProfilePictureKey, "attorney-journal-dev"); userTimeline.Items = await files.Where(x => x.Owner.Id == userId && x.Parent == null).Select( x => new TimelineItemModel { CreatedAt = x.CreatedAt, File = new ItemModel { Id = x.Id, Title = x.Title, Content = x.Content, FileExtension = x.FileExtension, MimeType = x.MimeType, ObjectKey = x.AmazonObjectKey, ObjectUrl = AmazonS3Helper.GenerateUrl(x.AmazonObjectKey, "attorney-journal-dev"), Type = x.Type, Viewed = x.Viewed, DateTaken = x.DateTaken }, Thumb = context.Files.Where(y => y.Parent == x).Select(y => new ItemModel { FileExtension = y.FileExtension, MimeType = y.MimeType, ObjectKey = y.AmazonObjectKey, ObjectUrl = GetImageAsBase64Url(AmazonS3Helper.GenerateUrl(y.AmazonObjectKey, "attorney-journal-dev")), DateTaken = x.DateTaken }).FirstOrDefault() }).OrderByDescending(x => x.CreatedAt).ToListAsync(CancellationToken.None); if (showDummy) { userTimeline.Items.Add(new TimelineItemModel { CreatedAt = DateTime.Now, File = new ItemModel { Id = Guid.NewGuid(), Title = "test image", Content = "this is test image", Type = FileStorageType.Image, FileExtension = "jpg", MimeType = "img/jpg", ObjectUrl = "https://appwrk.com/wp-content/themes/appwrk_theme/images/testi_1.jpg", Viewed = true, DateTaken = DateTime.Now }, Thumb = new ItemModel { FileExtension = "jpg", MimeType = "img/jpg", ObjectUrl = GetImageAsBase64Url("https://appwrk.com/wp-content/themes/appwrk_theme/images/testi_1.jpg"), DateTaken = DateTime.Now } }); userTimeline.Items.Add(new TimelineItemModel { CreatedAt = DateTime.Now, File = new ItemModel { Id = Guid.NewGuid(), Title = "test video", Content = "this is test video", Type = FileStorageType.Video, ObjectUrl = "https://www.youtube.com/watch?v=e_04ZrNroTo", Viewed = true, DateTaken = DateTime.Now }, Thumb = new ItemModel { ObjectUrl = GetImageAsBase64Url("http://i3.ytimg.com/vi/e_04ZrNroTo/hqdefault.jpg"), DateTaken = DateTime.Now } }); } return(userTimeline); }
protected virtual string SaveUploadedFileToAws(ColumnField field, string strFileName, string contentType, System.IO.Stream stream) { return(AmazonS3Helper.SaveUploadedFileToAws(field.FtpUpload.AwsAccessKeyId, field.FtpUpload.GetDecryptedAwsSecretAccessKey(Map.GetConfigDatabase()), field.FtpUpload.DirectoryBasePath, strFileName)); }
public async Task <IActionResult> RegisterWithRegistrationCode([FromBody] RegisterViewModel model) { if (ModelState.IsValid) { //var availableCode = await _applicationDbContext.RegistrationCodes.FirstOrDefaultAsync(x => x.Code == model.RegistrationCode && !x.HasBeenUsed); //if (availableCode == null) //{ // ModelState.AddModelError(nameof(model.RegistrationCode), "Code is not valid or has been already used!"); // return Json(new HttpResponseMessageWithError(null, new List<string> { "Code is not valid or has been already used!" })); //} var user = new ApplicationUser { UserName = model.Email, Email = model.Email, DateOfAccident = model.DateOfAccident, Name = model.Name, Surname = model.Surname }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { var profileObjectKey = $"{Guid.NewGuid()}.jpg"; if (!string.IsNullOrWhiteSpace(model.ProfileImage)) { var bytes = Convert.FromBase64String(model.ProfileImage); var targetFilePath = Path.GetTempFileName(); using (var imageFile = new FileStream(targetFilePath, FileMode.Create)) { imageFile.Write(bytes, 0, bytes.Length); imageFile.Flush(); } using (var amazonClient = new AmazonS3Helper(_amazonS3)) { await amazonClient.UploadFileAsync(targetFilePath, profileObjectKey); } user.ProfilePictureKey = profileObjectKey; } // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, Request.Scheme); //TODO: cambiare con "Grazie per esserti registrato //await _emailSender.SendEmailAsync(model.Email, "Confirm your account", "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"); await _emailSender.SendEmailAsync( null, model.Email, "Welcome to Visual Evidence Recorder", "Welcome to Visual Evidence Recorder.", model.Name, model.Surname); //availableCode.HasBeenUsed = true; //availableCode.UserId = user.Id; await _applicationDbContext.SaveChangesAsync(); //var ticket = await AuthorizationController.CreateTicketAsync(_signInManager, _identityOptions, new OpenIdConnectRequest { GrantType = "password", ClientId = "postman", Username = model.Email, Password = model.Password }, user); //var signInResult = SignIn(ticket.Principal, ticket.Properties, ticket.AuthenticationScheme); //return Json(signInResult); return(Json(this.SetResponseAndReturn(statusCode: HttpStatusCode.Created))); } return(Json(this.SetResponseAndReturn(result.Errors.Select(x => $"{x.Code} - {x.Description}").ToList(), HttpStatusCode.BadRequest))); } return(Json(this.SetResponseAndReturn(statusCode: HttpStatusCode.BadRequest))); }