public async Task <bool> UploadFileAsync(InputFileServiceModel file) { Guard.AgainstNullObject <FileException>(file, nameof(file)); Guard.AgainstEmptyString <FileException>(file.FileName, nameof(file.FileName)); Guard.AgainstInvalidWindowsCharacters <FileException>(file.FileName, nameof(file.FileName)); var firstChunk = file.Chunk == 0; var lastChunk = file.Chunk == file.Chunks - 1; // first chunk if (firstChunk) { // create blob row var blobCreated = await this.fileDapper.CreateBlobAsync(file); if (file.Chunks > 1) { return(blobCreated); } } // append til get to the last chunk (middle) if (!firstChunk && !lastChunk) { return(await this.fileDapper.AppendChunkToBlobAsync(file)); } // if last chunk append and save file if (!firstChunk) { await this.fileDapper.AppendChunkToBlobAsync(file); } var insertedFileId = 0; var insertedMessageId = 0; var existingFileId = await this.fileDapper.DoesFileWithSameNameExistInFolder( file.CatalogId, file.FolderId, file.FileName, file.FileType); file.CreateDate = DateTimeOffset.UtcNow; if (existingFileId > 0) { // file with name does exist in folder // add as new file with new fileName // check if we need to change filename var filesInFolder = await this.GetFilesByFolderIdAsync(file.CatalogId, file.FolderId, file.EmployeeId); if (filesInFolder.Any(f => f.FileName == file.FileName && f.FileType == file.FileType)) { var oldFileName = file.FileName; this.RenameFileName(filesInFolder, file: file); // TODO: refactor to EF and move messages out var(FileId, MessageId) = await this.fileDapper.SaveCompletedUploadAsync(file, oldFileName); insertedFileId = FileId; insertedMessageId = MessageId; } if (insertedFileId < 1) { return(false); } } else // file with name does NOT exist in folder { // TODO: refactor to EF and move messages out var(FileId, MessageId) = await this.fileDapper.SaveCompletedUploadAsync(file); insertedFileId = FileId; insertedMessageId = MessageId; if (insertedFileId < 1) { return(false); } } if (insertedFileId > 0) { // TODO: refactor when move to EF var messageData = new FileUploadedMessage { FileId = insertedFileId, Name = file.FileName, Type = file.ContentType }; await this.publisher.Publish(messageData); await this.messageService.MarkMessageAsPublishedAsync(insertedMessageId); } return(true); }
public static async Task Run([QueueTrigger("summerschoolqueue", Connection = "AzureWebJobsStorage")] FileUploadedMessage message, ILogger log) { log.LogInformation($"C# Queue trigger function processed: {message.FileIdentifier}"); string conectionstring = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); var storageAccount = CloudStorageAccount.Parse(conectionstring); var blobClient = storageAccount.CreateCloudBlobClient(); var container = blobClient.GetContainerReference("summerschoolcontainer1"); var blob = container.GetBlockBlobReference(message.FriendlyName + ".csv"); string line = await blob.DownloadTextAsync(); string[] users = line.Split(new Char[] { '\n' }); foreach (string s in users) { log.LogInformation($"Mes: {s}"); } var str = Environment.GetEnvironmentVariable("sqlbd_connection"); using (SqlConnection conn = new SqlConnection(str)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "INSERT INTO [File] (Email, Url) VALUES (@Email, @Url)"; cmd.Prepare(); cmd.Parameters.AddWithValue("@Email", message.Email); cmd.Parameters.AddWithValue("@Url", message.FileIdentifier); cmd.ExecuteNonQuery(); string query2 = "Select @@Identity as newId from [File]"; cmd.CommandText = query2; cmd.ExecuteNonQuery(); int newId = Convert.ToInt32(cmd.ExecuteScalar()); log.LogInformation($"Message: {users.Length}"); for (int i = 0; i < users.Length - 1; i++) { SqlCommand command = new SqlCommand(); command.Connection = conn; command.Parameters.Add("@FileId", SqlDbType.Int, Int32.MaxValue).Value = newId; command.Parameters.Add("@RowId", SqlDbType.Int, Int32.MaxValue).Value = i + 1; string[] data = users[i].Split(new Char[] { ';' }); string q = ""; log.LogInformation($"Message: {i}"); q = "INSERT INTO [ProcessedRow] (FileId, FirstName, LastName, Age, RowId) VALUES (@FileId, @FirstName, @LastName, @Age, @RowId)"; if (data[0].GetType() != typeof(string)) { q = " INSERT INTO [FailedRow] (FileId, RowId, FieldName, FieldValue) VALUES ( @FileId, @RowId, @FieldName, @FieldValue)"; command.Parameters.Add("@FieldName", SqlDbType.NVarChar, -1).Value = "FirstName"; command.Parameters.Add("@FieldValue", SqlDbType.NVarChar, -1).Value = data[0].ToString(); } else if (data[1].GetType() != typeof(string)) { q = " INSERT INTO [FailedRow] (FileId, RowId, FieldName, FieldValue) VALUES ( @FileId, @RowId, @FieldName, @FieldValue)"; command.Parameters.Add("@FieldName", SqlDbType.NVarChar, -1).Value = "LastName"; command.Parameters.Add("@FieldValue", SqlDbType.NVarChar, -1).Value = data[1].ToString(); } else if (!Int32.TryParse(data[2], out int x)) { q = " INSERT INTO [FailedRow] (FileId, RowId, FieldName, FieldValue) VALUES ( @FileId, @RowId, @FieldName, @FieldValue)"; command.Parameters.Add("@FieldName", SqlDbType.NVarChar, -1).Value = "Age"; command.Parameters.Add("@FieldValue", SqlDbType.NVarChar, -1).Value = data[2].ToString(); } else { command.Parameters.Add("@FirstName", SqlDbType.NVarChar, -1).Value = data[0]; command.Parameters.Add("@LastName", SqlDbType.NVarChar, -1).Value = data[1]; command.Parameters.Add("@Age", SqlDbType.Int, Int32.MaxValue).Value = data[2]; } command.CommandText = q; command.Prepare(); command.ExecuteNonQuery(); } log.LogInformation($"Message: {newId}"); conn.Close(); } }