public override bool IsUserInRole(string username, string roleName)
        {
            bool outputResult = false;

            // Находим пользователя
            using (UploadContext _db = new UploadContext())
            {
                try
                {
                    // Получаем пользователя
                    User user = (from u in _db.Users
                                 where u.Login == username
                                 select u).FirstOrDefault();
                    if (user != null)
                    {
                        // получаем роль
                        Role userRole = _db.Roles.Find(user.RoleId);

                        //сравниваем
                        if (userRole != null && userRole.Name == roleName)
                        {
                            outputResult = true;
                        }
                    }
                }
                catch
                {
                    outputResult = false;
                }
            }
            return(outputResult);
        }
        public override string[] GetRolesForUser(string login)
        {
            string[] role = new string[] { };
            using (UploadContext _db = new UploadContext())
            {
                try
                {
                    // Получаем пользователя
                    User user = (from u in _db.Users
                                 where u.Login == login
                                 select u).FirstOrDefault();
                    if (user != null)
                    {
                        // получаем роль
                        Role userRole = _db.Roles.Find(user.RoleId);

                        if (userRole != null)
                        {
                            role = new string[] { userRole.Name };
                        }
                    }
                }
                catch
                {
                    role = new string[] { };
                }
            }
            return(role);
        }
Exemple #3
0
        void upload_one(PutMetricDataRequest pmdr, UploadContext ctx)
        {
            var cw_client_ = new Amazon.CloudWatch.AmazonCloudWatchClient(config_.AWSMetricsCredentials.Akid, config_.AWSMetricsCredentials.SecretKey, config_.AWSMetricsCredentials.SessionToken, region_);
            CancellationTokenSource cts = new CancellationTokenSource();
            var token = cts.Token;

            cw_client_.PutMetricDataAsync(pmdr, token).ContinueWith
            (
                (pmdresp) =>
            {
                if (pmdresp.Result.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    return;
                }

                //Do error handling
                ctx.attempts++;
                var msg = pmdresp.Exception.Message;
                if (ctx.created > DateTime.Now.AddMinutes(-30))
                {
                    StdErrorOut.Instance.StdError("Metrics upload failed. Error Mesg : " + msg);
                }
                else
                {
                    if (ctx.attempts == 5)
                    {
                        StdErrorOut.Instance.StdOut(LogLevel.warn, string.Format("Metrics upload failed. | Code: {0} | Message: {1} | Request was: {3}", pmdresp.Result.HttpStatusCode, msg, pmdr.MetricData.ToString()));
                    }

                    executor.Schedule(() => { upload_one(pmdr, ctx); }, retry_delay_);
                }
            }
            );
        }
Exemple #4
0
        private void UploadVisitsConcurrently(
            UploadContext uploadContext,
            List <FieldVisitInfo> visitsToAppend,
            List <FieldVisitInfo> duplicateVisits,
            ref bool isPartial,
            ref bool isFailure)
        {
            var semaphore = new SemaphoreSlim(Context.MaximumConcurrentRequests);

            var localIsPartial = false;
            var localIsFailure = false;

            Task.WhenAll(visitsToAppend.GroupBy(v => v.LocationInfo.LocationIdentifier).Select(async grouping =>
            {
                using (await LimitedConcurrencyContext.EnterContextAsync(semaphore))
                {
                    await Task.Run(() =>
                                   UploadLocationVisits(
                                       grouping
                                       .OrderBy(v => v.StartDate)
                                       .ThenBy(v => v.EndDate)
                                       .ToList(),
                                       grouping.Key,
                                       uploadContext,
                                       () => localIsPartial = true,
                                       () => localIsFailure = true,
                                       duplicateVisits.Add)
                                   , CancellationToken);
                }
            })).Wait(CancellationToken);

            isPartial |= localIsPartial;
            isFailure |= localIsFailure;
        }
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (UploadContext db = new UploadContext())
                {
                    User existUser = db.Users.FirstOrDefault(u => u.Login == model.UserName);
                    if (existUser != null)
                    {
                        ModelState.AddModelError("", "This login already exist. Enter another login");
                        return(View());
                    }

                    Role role = db.Roles.FirstOrDefault(r => r.Name == "User");

                    if (role != null)
                    {
                        var user = new User {
                            Login = model.UserName, Password = model.Password, Role = role, RoleId = role.Id
                        };
                        db.Users.Add(user);
                        db.SaveChanges();

                        return(RedirectToAction("Login", "Account"));
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemple #6
0
        private void UploadVisitsConcurrently(
            UploadContext uploadContext,
            List <FieldVisitInfo> visitsToAppend,
            List <FieldVisitInfo> duplicateVisits,
            ref bool isPartial,
            ref bool isFailure)
        {
            var semaphore = new SemaphoreSlim(Context.MaximumConcurrentRequests);

            var localIsPartial = false;
            var localIsFailure = false;

            Task.WhenAll(visitsToAppend.Select(async visit =>
            {
                using (await LimitedConcurrencyContext.EnterContextAsync(semaphore))
                {
                    await Task.Run(() =>
                                   UploadVisit(
                                       visit,
                                       uploadContext,
                                       () => localIsPartial = true,
                                       () => localIsFailure = true,
                                       () => duplicateVisits.Add(visit))
                                   , CancellationToken);
                }
            })).Wait(CancellationToken);

            isPartial |= localIsPartial;
            isFailure |= localIsFailure;
        }
        public JsonResult Upload()
        {
            //Dictionary<string, byte[]> dictionary = new Dictionary<string, byte[]>();
            //System.Threading.Tasks.Parallel.ForEach(Request.Files.AllKeys, (file) =>
            //{
            //    System.Web.HttpPostedFileBase upload = Request.Files[file];
            //    //считаем загруженный файл в массив
            //    byte[] arrayBytes = null;
            //    lock (this.lockObject)
            //    {
            //        arrayBytes = new byte[upload.ContentLength];
            //        upload.InputStream.Read(arrayBytes, 0, arrayBytes.Length);
            //        dictionary.Add(upload.FileName, arrayBytes);
            //    }
            //});



            User user = new UploadContext().Users.Where(m => m.Login == HttpContext.User.Identity.Name).FirstOrDefault();
            MultithreadProcessingFiles pr = new MultithreadProcessingFiles(user);

            pr.Processing(Request.Files);

            return(Json(pr.GetListUploadedFiles_()));
        }
Exemple #8
0
        public void TestCreateAndPopulate()
        {
            UploadContext context = new UploadContext();

            UploadHeader header = new UploadHeader();

            header.Begin = DateTime.Now;

            context.UploadHeaders.Add(header);

            DateTime       dt      = DateTime.Now;
            int            minutes = 0;
            IList <String> tables  = new List <String>()
            {
                "IN_GUEST", "IN_TRN", "IN_MSG", "IN_RES"
            };

            foreach (String table in tables.OrderBy(s => s))
            {
                minutes++;
                UploadDetail dtl = new UploadDetail()
                {
                    TableName = table, Begin = dt.AddMinutes(minutes++), End = dt.AddMinutes(minutes++), UploadHeader = header
                };
                context.UploadDetails.Add(dtl);
            }
            context.SaveChanges();
            TestContext.WriteLine("Done - {0}", minutes);
        }
Exemple #9
0
        private byte[] GetUploadFileBytes(AppendedResults singleResult, UploadContext uploadContext)
        {
            var jsonBytes = Encoding.UTF8.GetBytes(singleResult.ToJson());

            if (!uploadContext.HasAttachments)
            {
                return(jsonBytes);
            }

            using (var stream = new MemoryStream())
            {
                using (var archive = new ZipArchive(stream, ZipArchiveMode.Update, true))
                {
                    AddArchiveEntry(archive, new Attachment
                    {
                        Content       = jsonBytes,
                        Path          = uploadContext.UploadedFilename,
                        LastWriteTime = DateTimeOffset.UtcNow,
                        ByteSize      = jsonBytes.Length
                    });

                    foreach (var attachment in uploadContext.Attachments)
                    {
                        AddArchiveEntry(archive, attachment);
                    }
                }

                stream.Position = 0;

                var zipBytes = stream.GetBuffer();

                return(zipBytes);
            }
        }
        public void SetUp()
        {
            _reader  = TestInitializer.ServiceProvider.GetService <IDwhExtractReader>();
            _context = TestInitializer.ServiceProvider.GetService <UploadContext>();
            var econtext = TestInitializer.ServiceProvider.GetService <ExtractsContext>();

            _diffLogs = econtext.DiffLogs.ToList();
        }
 public SqlTableProcessorEventHandler(String connectionName)
 {
     Context      = new UploadContext(connectionName);
     Header       = new UploadHeader();
     Header.Begin = DateTime.Now;
     Context.UploadHeaders.Add(Header);
     Context.SaveChanges();
 }
        public FileResult GetFile(long Id)
        {
            UploadedFile uf;

            using (UploadContext db = new UploadContext())
            {
                uf = db.UploadedFiles.Where(x => x.Id == Id).FirstOrDefault();
            }
            return(File(uf.FileContent, uf.MimeType, uf.FullFileName));
        }
Exemple #13
0
        private static string ComposeUploadedFilename(UploadContext uploadContext, FieldVisitInfo visit)
        {
            var uploadedFilename = uploadContext.HasAttachments
                ? ComposeSafeAttachmentUploadFilename(uploadContext)
                : uploadContext.AppendedResults.AppendedVisits.Count <= 1
                    ? uploadContext.UploadedFilename
                    : $"{Path.GetFileName(uploadContext.Path)}.{visit.LocationInfo.LocationIdentifier}-{visit.StartDate.Date:yyyy-MM-dd}.json";

            return(SanitizeFilename(uploadedFilename));
        }
Exemple #14
0
    private async Task StoreAsyncCore(FileIdentifier id, MultipartReader reader, CancellationToken cancellationToken)
    {
        // We need to manually read each part of the request. The browser may do as it likes and send whichever part first,
        // which means we have to build up the metadata incrementally and can only write it later

        UploadContext  uploadContext = new UploadContext(id);
        StoredMetadata?metadata      = null;

        try {
            MultipartSection section = await reader.ReadNextSectionAsync(cancellationToken);

            while (section != null)
            {
                await this.ProcessSectionAsync(uploadContext, section, cancellationToken);

                section = await reader.ReadNextSectionAsync(cancellationToken);
            }

            if (uploadContext.MetadataFactory.IsComplete())
            {
                metadata = uploadContext.MetadataFactory.Build();
            }

            if (metadata == null)
            {
                throw new InvalidOperationException("Metadata is incomplete - file is not uploaded or expiration missing");
            }

            await this.StoreMetadataAsync(id, metadata, cancellationToken);

            this._logger.LogInformation(LogEvents.NewUpload, "Completed: New upload of file {0} to id {1} [{2:s}]", metadata.OriginalFileName, id, DateTime.UtcNow);
        }
        catch (OperationCanceledException ex) {
            this._logger.LogWarning(LogEvents.UploadCancelled, ex, "Upload failed due to cancellation");

            this.TryCleanup(id);

            // No use rethrowing the exception, we're done anyway.
        }
        catch (InvalidDataException ex) {
            this._logger.LogError(LogEvents.UploadFailed, ex, "Upload failed due to file size exceeding");

            this.TryCleanup(id);

            throw new UploadFailedException("File size exceeded of " + reader.BodyLengthLimit.GetValueOrDefault().Bytes().Megabytes);
        }
        catch (Exception ex) when(!(ex is UploadCryptoArgumentOrderException))
        {
            this._logger.LogError(LogEvents.UploadFailed, ex, "Upload failed due to exception");

            this.TryCleanup(id);

            throw new UploadFailedException("Unable to complete upload", ex);
        }
    }
Exemple #15
0
        /// <summary>
        /// Accepts or rejects the file upload.
        /// </summary>
        protected override bool AcceptUpload(ConnectedClient client, RelativePath path, out object uploadContext)
        {
            UploadContext context = new UploadContext
            {
                FileName = GetClientInstance(client).PathBuilder.GetAbsolutePath(path),
                IsConfig = path.TopFolder == TopFolder.Agent && path.AppFolder == AppFolder.Temp &&
                           path.Path.StartsWith(AgentConst.UploadConfigPrefix)
            };

            uploadContext = context;
            return(context.IsConfig || path.TopFolder == TopFolder.Comm && path.AppFolder == AppFolder.Cmd);
        }
Exemple #16
0
        /// <summary>
        /// Processes the successfully uploaded file.
        /// </summary>
        protected override void ProcessFile(ConnectedClient client, RelativePath path, object uploadContext)
        {
            UploadContext context = (UploadContext)uploadContext;

            if (context.IsConfig)
            {
                if (!GetClientInstance(client).UnpackConfig(context.FileName))
                {
                    throw new ProtocolException(ErrorCode.InternalServerError);
                }
            }
        }
Exemple #17
0
    private async Task ProcessSectionAsync(UploadContext uploadContext, MultipartSection section, CancellationToken cancellationToken)
    {
        ContentDispositionHeaderValue contentDisposition = ContentDispositionHeaderValue.Parse(section.ContentDisposition);

        if (contentDisposition.FileName != null)
        {
            await this.ProcessFileSectionAsync(uploadContext, section, contentDisposition, cancellationToken);

            return;
        }

        await this.ProcessFormSectionAsync(uploadContext, section, contentDisposition);
    }
        public StreamingController(ILogger <StreamingController> logger,
                                   UploadContext context, IConfiguration config)
        {
            _logger        = logger;
            _context       = context;
            _fileSizeLimit = config.GetValue <long>("LargeFileSizeLimit");

            // To save physical files to a path provided by configuration:
            _targetFilePath = config.GetValue <string>("StoredFilesPath");

            // To save physical files to the temporary files folder, use:
            //_targetFilePath = Path.GetTempPath();
        }
Exemple #19
0
        public void Init()
        {
            var config = new ConfigurationBuilder()
                         .AddJsonFile("appsettings.json")
                         .Build();
            var connectionString = config["ConnectionStrings:DwapiConnection"];

            _serviceProvider = new ServiceCollection()
                               .AddDbContext <Dwapi.SettingsManagement.Infrastructure.SettingsContext>(o => o.UseSqlServer(connectionString))
                               .AddDbContext <UploadContext>(o => o.UseSqlServer(connectionString))
                               .BuildServiceProvider();

            _context = _serviceProvider.GetService <UploadContext>();
        }
Exemple #20
0
 public Task Execute(IJobExecutionContext context)
 {
     using (var scope = _provider.CreateScope())
     {
         _context = scope.ServiceProvider.GetService <UploadContext>();
         _logger.LogInformation("File Processing Job Initiated: " + DateTime.Now.ToString("dddd, MMMM dd, yyyy HH:mm:ss.fffK"));
         List <Upload> unprocessedUploads = GetUnprocessedUploads();
         _logger.LogInformation("Count of files requiring processing: " + unprocessedUploads.Count);
         if (unprocessedUploads.Count > 0)
         {
             ProcessFiles(unprocessedUploads);
         }
         _logger.LogInformation("File Processing Job Completed: " + DateTime.Now.ToString("dddd, MMMM dd, yyyy HH:mm:ss.fffK"));
     }
     return(Task.CompletedTask);
 }
        public IEnumerable <AdminData> Get()
        {
            IEnumerable <AdminData> entries = null;

            using (UploadContext db = new UploadContext())
            {
                entries = db.UploadedFiles.Select(x => new AdminData {
                    Id               = x.Id,
                    FileName         = x.FullFileName,
                    WhoDidFileUpload = x.User.Login,
                    CoreCreator      = x.OldInformationFile == null ? null : x.OldInformationFile.CoreCreator,
                    CoreRevision     = x.OldInformationFile.CoreRevision == null ? null: x.OldInformationFile.CoreRevision,
                    CoreCreated      = x.OldInformationFile == null ? new DateTimeOffset() : x.OldInformationFile.CoreCreated,
                    CoreModified     = x.OldInformationFile == null ? new DateTimeOffset() : x.OldInformationFile.CoreModified
                }).ToList <AdminData>();
            }
            return(entries);
        }
Exemple #22
0
    private async Task ProcessFileSectionAsync(UploadContext uploadContext, MultipartSection section, ContentDispositionHeaderValue contentDisposition, CancellationToken cancellationToken)
    {
        string cleanName = HeaderUtilities.RemoveQuotes(contentDisposition.Name).Value;
        string fileName  = HeaderUtilities.RemoveQuotes(contentDisposition.FileName).Value;

        if (cleanName == nameof(UploadModel.File))
        {
            this._logger.LogInformation(LogEvents.NewUpload, "New upload of file {0} to id {1} [{2:s}]", fileName, uploadContext.Identifier, DateTime.UtcNow);

            uploadContext.MetadataFactory.SetOriginalFileName(fileName);

            await this.StoreDataAsync(uploadContext.Identifier, section.Body, uploadContext.PasswordSetting, cancellationToken).ConfigureAwait(false);

            uploadContext.HasUploadedFile = true;
        }
        else
        {
            this._logger.LogWarning(LogEvents.UploadIncomplete, "{0}: Unknown file '{1}' with file name '{2}'. Skipping.", uploadContext.Identifier, fileName, cleanName);
        }
    }
Exemple #23
0
        private void UploadLocationVisits(
            List <FieldVisitInfo> visits,
            string locationIdentifier,
            UploadContext uploadContext,
            Action partialAction,
            Action failureAction,
            Action <FieldVisitInfo> duplicateAction)
        {
            if (visits.Count > 1)
            {
                Log.Info($"Uploading {"visit".ToQuantity(visits.Count)} to location '{locationIdentifier}' from {visits.First().StartDate:O} to {visits.Last().EndDate:O}");
            }

            foreach (var visit in visits)
            {
                UploadVisit(
                    visit,
                    uploadContext,
                    partialAction,
                    failureAction,
                    duplicateAction);
            }
        }
        private bool ValidateUser(string login, string password)
        {
            bool isValid = false;

            using (UploadContext _db = new UploadContext())
            {
                try
                {
                    User user = (from u in _db.Users
                                 where u.Login == login && u.Password == password
                                 select u).FirstOrDefault();

                    if (user != null)
                    {
                        isValid = true;
                    }
                }
                catch
                {
                    isValid = false;
                }
            }
            return(isValid);
        }
        private void ParseOrThrow()
        {
            origPreloadedBody = OrigWorker.GetPreloadedEntityBody();
            string contentTypeHeader = OrigWorker.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentType);
            string contentLengthHeader = OrigWorker.GetKnownRequestHeader(HttpWorkerRequest.HeaderContentLength);
            string transferEncodingHeader = OrigWorker.GetKnownRequestHeader(HttpWorkerRequest.HeaderTransferEncoding);
            if (contentLengthHeader != null)
            {
                origContentLength = Int64.Parse(contentLengthHeader);
            }

            if (Config.Current.DebugDirectory != null)
            {
                string logEntityBodyBaseName = Path.Combine(Config.Current.DebugDirectory.FullName,
                                                            DateTime.Now.Ticks.ToString());
                LogEntityBodyStream = File.Create(logEntityBodyBaseName + ".body");
                LogEntityBodySizesStream = File.CreateText(logEntityBodyBaseName + ".sizes");
                LogEntityBodySizesStream.WriteLine(contentTypeHeader);
                LogEntityBodySizesStream.WriteLine(contentLengthHeader);
                if (origPreloadedBody != null)
                {
                    LogEntityBodyStream.Write(origPreloadedBody, 0, origPreloadedBody.Length);
                    LogEntityBodySizesStream.WriteLine(origPreloadedBody.Length);
                }
                else
                {
                    LogEntityBodySizesStream.WriteLine(0);
                }
            }

            FieldNameTranslator translator = new FieldNameTranslator();
            if (MultiRequestControlID == null && UploadState != null)
            {
                UploadState.BytesTotal += origContentLength;
            }
            if (log.IsDebugEnabled) log.Debug("=" + contentLengthHeader + " -> " + origContentLength);

            boundary = System.Text.Encoding.ASCII.GetBytes("--" + GetAttribute(contentTypeHeader, "boundary"));
            if (log.IsDebugEnabled) log.Debug("boundary=" + System.Text.Encoding.ASCII.GetString(boundary));

            string charset = GetAttribute(contentTypeHeader, "charset");
            if (charset != null)
            {
                try
                {
                    System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(charset);
                    ContentEncoding = encoding;
                }
                catch (NotSupportedException)
                {
                    if (log.IsDebugEnabled) log.Debug("Ignoring unsupported charset " + charset + ".  Using utf-8.");
                }
            }
            else
            {
                ContentEncoding = HttpContext.Current.Response.ContentEncoding;
            }
            preloadedEntityBodyStream = new MemoryStream();
            Hashtable storageConfigStreamTable = new Hashtable();
            Stream postBackIDStream = null;
            outputStream = preloadedEntityBodyStream;
            readPos = writePos = parsePos = 0;
            while (CopyUntilBoundary())
            {
                // If we were writing to a file, close it
                if (outputStream == fileStream && outputStream != null)
                {
                    outputStream.Close();
                }

                // If we were receiving the value generated by the HiddenPostBackID control, set the postback ID.
                if (postBackIDStream != null)
                {
                    postBackIDStream.Seek(0, System.IO.SeekOrigin.Begin);
                    StreamReader sr = new System.IO.StreamReader(postBackIDStream);
                    translator.PostBackID = sr.ReadToEnd();
                    postBackIDStream = null;
                }

                // parse the headers
                string name = null, fileName = null, contentType = null;
                if (boundary[0] != (byte)'\r')
                {
                    byte[] newBoundary = new byte[boundary.Length + 2];
                    Buffer.BlockCopy(boundary, 0, newBoundary, 2, boundary.Length);
                    newBoundary[0] = (byte)'\r';
                    newBoundary[1] = (byte)'\n';
                    boundary = newBoundary;
                }
                else
                {
                    GetLine(); // Blank line
                }
                GetLine(); // boundary line
                string header;
                while (null != (header = GetLine()))
                {
                    if (log.IsDebugEnabled) log.Debug("header=" + header);
                    int colonPos = header.IndexOf(':');
                    if (colonPos < 0)
                    {
                        break;
                    }
                    string headerName = header.Substring(0, colonPos);
                    if (String.Compare(headerName, "Content-Disposition", true) == 0)
                    {
                        name = GetAttribute(header, "name");
                        fileName = GetAttribute(header, "filename");
                    }
                    else if (String.Compare(headerName, "Content-Type", true) == 0)
                    {
                        contentType = header.Substring(colonPos + 1).Trim();
                    }
                }
                if (log.IsDebugEnabled) log.Debug("name = " + name);
                if (log.IsDebugEnabled) log.Debug("fileName = " + fileName);
                if (log.IsDebugEnabled) log.DebugFormat("name = " + name);
                if (log.IsDebugEnabled) log.DebugFormat("fileName = " + fileName);
                string controlID = null;
                if (name == Config.Current.PostBackIDQueryParam && postBackIDStream == null)
                {
                    postBackIDStream = outputStream = new System.IO.MemoryStream();
                    readPos = parsePos; // Skip past the boundary and headers
                }
                else if (name != null
                    && null != (controlID = translator.ConfigFieldNameToControlID(name)))
                {
                    storageConfigStreamTable[controlID] = outputStream = new System.IO.MemoryStream();
                    readPos = parsePos; // Skip past the boundary and headers
                }
                else if (name != null
                         && null != (controlID = translator.FileFieldNameToControlID(name)))
                {
                    if (log.IsDebugEnabled) log.DebugFormat("name != null && controlID != null");
                    if (UploadState == null)
                    {
                        UploadState = UploadStateStore.OpenReadWriteOrCreate(translator.FileFieldNameToPostBackID(name));
                        if (transferEncodingHeader != "chunked")
                            UploadState.Status = UploadStatus.NormalInProgress;
                        else
                            UploadState.Status = UploadStatus.ChunkedInProgress;
                        UploadState.BytesRead += grandTotalBytesRead;
                        UploadState.BytesTotal += origContentLength;
                    }

                    UploadStorageConfig storageConfig = null;

                    if (UploadState.MultiRequestObject != null)
                    {
                        string secureStorageConfigString = UploadState.MultiRequestObject as string;
                        if (secureStorageConfigString != null)
                        {
                            storageConfig = UploadStorage.CreateUploadStorageConfig();
                            storageConfig.Unprotect(secureStorageConfigString);
                            if (log.IsDebugEnabled) log.DebugFormat("storageConfig[tempDirectory]={0}", storageConfig["tempDirectory"]);
                        }
                    }
                    string configID = translator.FileIDToConfigID(controlID);
                    MemoryStream storageConfigStream = storageConfigStreamTable[configID] as MemoryStream;
                    if (storageConfigStream != null)
                    {
                        storageConfigStream.Seek(0, System.IO.SeekOrigin.Begin);
                        StreamReader sr = new System.IO.StreamReader(storageConfigStream);
                        string secureStorageConfigString = sr.ReadToEnd();
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("storageConfigStream = " + secureStorageConfigString);
                        }
                        storageConfig = UploadStorage.CreateUploadStorageConfig();
                        storageConfig.Unprotect(secureStorageConfigString);

                        // Write out a part for the config hidden field
                        if (log.IsDebugEnabled) log.DebugFormat("Calling WriteReplacementFormField({0}, {1})", configID, secureStorageConfigString);
                        WriteReplacementFormField(configID, secureStorageConfigString);
                        // Remove the stream from the table, so we don't write the replacement field again.
                        storageConfigStreamTable.Remove(configID);
                    }

                    if (fileName != null)
                    {
                        if (log.IsDebugEnabled) log.DebugFormat("filename != null");
                        if (log.IsDebugEnabled) log.Debug("Calling UploadContext.Current.CreateUploadedFile(" + controlID + "...)");
                        UploadContext tempUploadContext = new UploadContext();
                        tempUploadContext._ContentLength = origContentLength;
                        UploadedFile uploadedFile
                            = UploadStorage.CreateUploadedFile(tempUploadContext, controlID, fileName, contentType, storageConfig);
                        UploadState.Files.Add(controlID, uploadedFile);
                        if (MultiRequestControlID == null)
                            RegisterFilesForDisposal(controlID);
                        outputStream = fileStream = uploadedFile.CreateStream();
                        readPos = parsePos; // Skip past the boundary and headers

                        // If the client-specified content length is too large, we set the status to
                        // RejectedRequestTooLarge so that progress displays will stop.  We do this after
                        // having created the UploadedFile because that is necessary for the progress display
                        // to find the uploadContext.
                        if (origContentLength > UploadHttpModule.MaxRequestLength)
                        {
                            if (log.IsDebugEnabled) log.Debug("contentLength > MaxRequestLength");
                            throw new UploadTooLargeException(UploadHttpModule.MaxRequestLength, origContentLength);
                        }

                        // Write out a replacement part that just contains the filename as the value.
                        WriteReplacementFormField(controlID, fileName);
                    }
                    else
                    {
                        if (log.IsDebugEnabled) log.DebugFormat("filename == null");
                        // Since filename==null this must just be a hidden field with a name that
                        // looks like a file field.  It is just an indication that when this request
                        // ends, the associated uploaded files should be disposed.
                        if (MultiRequestControlID == null)
                        {
                            if (log.IsDebugEnabled) log.DebugFormat("MultiRequestControlID == null");
                            RegisterFilesForDisposal(controlID);
                        }
                        outputStream = preloadedEntityBodyStream;
                    }
                }
                else
                {
                    outputStream = preloadedEntityBodyStream;
                }
            }
            if (log.IsDebugEnabled) log.Debug("Done parsing.");
            outputStream.WriteByte(10);
            outputStream.Close();
            preloadedEntityBody = preloadedEntityBodyStream.ToArray();
            preloadedEntityBodyStream = null;
            if (grandTotalBytesRead < origContentLength)
            {
                bool isClientConnected = false;
                try
                {
                    isClientConnected = OrigWorker.IsClientConnected();
                }
                catch (Exception)
                {
                    // Mono throws an exception if the client is no longer connected.
                }
                if (isClientConnected)
                {
                    throw new HttpException (400, String.Format("Data length ({0}) is shorter than Content-Length ({1}) and client is still connected after {2} secs.",
                                                                grandTotalBytesRead, origContentLength, Math.Round(UploadState.TimeElapsed.TotalSeconds)));
                }
                else
                {
                    throw new HttpException (400, String.Format("Client disconnected after receiving {0} of {1} bytes in {2} secs -- user probably cancelled upload.",
                                                                grandTotalBytesRead, origContentLength, Math.Round(UploadState.TimeElapsed.TotalSeconds)));
                }
            }
        }
Exemple #26
0
 public DwhExtractReader(UploadContext context)
 {
     _context = context;
 }
Exemple #27
0
 public EmrMetricReader(UploadContext context)
 {
     _context = context;
 }
Exemple #28
0
 private static string ComposeSafeAttachmentUploadFilename(UploadContext uploadContext)
 {
     // AQTS really needs the uploaded filename to end with ".zip" if it has attachments
     return(Path.GetFileNameWithoutExtension(uploadContext.Path) + ".zip");
 }
Exemple #29
0
        private void UploadVisit(FieldVisitInfo visit,
                                 UploadContext uploadContext,
                                 Action partialAction,
                                 Action failureAction,
                                 Action duplicateAction = null)
        {
            if (ShouldSkipConflictingVisits(visit))
            {
                partialAction();
                return;
            }

            if (Context.DryRun)
            {
                Log.Warn($"Dry-run: Would upload '{visit.FieldVisitIdentifier}'");
                return;
            }

            var singleResult = new AppendedResults
            {
                FrameworkAssemblyQualifiedName  = uploadContext.AppendedResults.FrameworkAssemblyQualifiedName,
                PluginAssemblyQualifiedTypeName = uploadContext.AppendedResults.PluginAssemblyQualifiedTypeName,
                AppendedVisits = new List <FieldVisitInfo> {
                    visit
                }
            };

            try
            {
                using (var stream = new MemoryStream(GetUploadFileBytes(singleResult, uploadContext)))
                {
                    var uploadedFilename = ComposeUploadedFilename(uploadContext, visit);

                    var response = Client.Acquisition.PostFileWithRequest(stream, uploadedFilename,
                                                                          new PostVisitFileToLocation
                    {
                        LocationUniqueId = Guid.Parse(visit.LocationInfo.UniqueId)
                    });

                    Log.Info($"Uploaded '{uploadedFilename}' {visit.FieldVisitIdentifier} to '{visit.LocationInfo.LocationIdentifier}' using {response.HandledByPlugin.Name} plugin");
                }
            }
            catch (WebServiceException exception)
            {
                if (IsDuplicateAttachmentException(exception))
                {
                    Log.Info($"{uploadContext.UploadedFilename}: Skipping already uploaded content.");
                    return;
                }

                if (duplicateAction != null && IsDuplicateFailure(exception))
                {
                    Log.Warn($"{uploadContext.UploadedFilename}: Saving {visit.FieldVisitIdentifier} for later retry: {exception.ErrorCode} {exception.ErrorMessage}");

                    duplicateAction();
                    return;
                }

                Log.Error($"{uploadContext.UploadedFilename}: {visit.FieldVisitIdentifier}: {exception.ErrorCode} {exception.ErrorMessage}");
                failureAction();
            }
        }
Exemple #30
0
        private UploadedResults UploadResultsConcurrently(UploadContext uploadContext)
        {
            var uploadedResults = new UploadedResults
            {
                IsPartial   = false,
                IsFailure   = false,
                PathsToMove = { uploadContext.Path }
            };

            if (!uploadContext.AppendedResults.AppendedVisits.Any())
            {
                Log.Warn($"No visits parsed from '{uploadContext.Path}'");

                return(uploadedResults);
            }

            var unknownVisits = uploadContext.AppendedResults
                                .AppendedVisits
                                .Where(IsUnknownLocation)
                                .ToList();

            var largeDurationVisits = uploadContext.AppendedResults
                                      .AppendedVisits
                                      .Where(visit => !unknownVisits.Contains(visit) && IsVisitDurationExceeded(visit))
                                      .ToList();

            var visitsToAppend = uploadContext.AppendedResults
                                 .AppendedVisits
                                 .Where(visit => !unknownVisits.Contains(visit) && !largeDurationVisits.Contains(visit))
                                 .ToList();

            if (unknownVisits.Any())
            {
                Log.Warn($"Skipping {unknownVisits.Count} visits for unknown locations");
            }

            if (largeDurationVisits.Any())
            {
                Log.Warn($"Skipping {largeDurationVisits.Count} visits exceeding /{nameof(Context.MaximumVisitDuration)}={Context.MaximumVisitDuration:g}");

                uploadedResults.PathsToMove.Add(SaveLargeVisits(uploadContext.AppendedResults, largeDurationVisits, uploadContext.Path));
            }

            if (!visitsToAppend.Any())
            {
                Log.Error($"None of the {uploadContext.AppendedResults.AppendedVisits.Count} visits can be imported.");
            }

            var isIncomplete = unknownVisits.Any() || largeDurationVisits.Any();
            var isFailure    = isIncomplete && !visitsToAppend.Any();
            var isPartial    = !isFailure && isIncomplete;

            uploadContext.UploadedFilename = Path.GetFileName(uploadContext.Path) + ".json";

            if (visitsToAppend.Any())
            {
                Log.Info($"Appending {visitsToAppend.Count} visits using {Context.MaximumConcurrentRequests} concurrent requests.");

                var duplicateVisits = new List <FieldVisitInfo>();

                UploadVisitsConcurrently(uploadContext, visitsToAppend, duplicateVisits, ref isPartial, ref isFailure);

                if (duplicateVisits.Any())
                {
                    for (var retryAttempts = 0; !CancellationToken.IsCancellationRequested &&
                         duplicateVisits.Any() &&
                         retryAttempts < Context.MaximumDuplicateRetry; ++retryAttempts)
                    {
                        Log.Info($"Retrying {duplicateVisits.Count} duplicate visits.");

                        visitsToAppend.Clear();
                        visitsToAppend.AddRange(duplicateVisits);
                        duplicateVisits.Clear();

                        UploadVisitsConcurrently(uploadContext, visitsToAppend, duplicateVisits, ref isPartial, ref isFailure);
                    }

                    if (duplicateVisits.Any())
                    {
                        Log.Error($"Could not resolve {duplicateVisits.Count} duplicate visits.");
                        isFailure = true;
                    }
                }
            }

            uploadedResults.IsPartial = isPartial;
            uploadedResults.IsFailure = isFailure;

            return(uploadedResults);
        }
Exemple #31
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private async Task OnMqttMessageReceived(MqttApplicationMessageReceivedEventArgs obj)
        {
            var payload = obj.ApplicationMessage.Payload;
            var topic   = obj.ApplicationMessage.Topic;

            //byte[] hexbytes = new byte[] {
            //    0x95,
            //    0x03,
            //    0x0E,
            //    0x56,0x78,
            //    0x01,
            //    0x00,0x88,
            //    0x00,0x1F,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01,
            //    0x00,0x00,0x21,0x04,0x00,0x00,0x0E,0x01,0x00,0x20,
            //    0x04,0x00,0x00,0x0F,0x01,0x00,0x0C,0x04,0x00,0x05,
            //    0x09,0x10,0x00,0x0C,0x04,0x00,0x12,0x76,0x90,0x00,
            //    0x16,0x02,0x00,0x78,0x00,0x08,0x00,0x00,0x09,0x00,
            //    0x00,0x19,0x00,0x00,0x07,0x01,0x08,0x00,0x0A,0x00,
            //    0x00,0x04,0x04,0xBC,0x4D,0xDE,0x5B,0x00,0x05,0x04,
            //    0xC7,0xFF,0x70,0x6F,0x00,0x06,0x02,0x07,0x7A,0x00,
            //    0x2E,0x04,0x00,0x00,0x00,0x00,0x00,0x2F,0x04,0x00,
            //    0x00,0x00,0x01,0x00,0x30,0x04,0x00,0x00,0x00,0x02,
            //    0x00,0x31,0x04,0x00,0x00,0x00,0x03,0x00,0x32,0x04,
            //    0x00,0x00,0x00,0x04,0x00,0x33,0x04,0x00,0x00,0x00,
            //    0x05,0x00,0x34,0x04,0x00,0x00,0x00,0x06,0x00,0x35,
            //    0x04,0x00,0x00,0x00,0x07,
            //    0x01,0xE1,
            //    0x59
            //};
            byte[] hexBytes = new byte[]  {
                0x95, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0xA9, 0x00, 0x1F, 0x04, 0x80, 0x05, 0x05, 0x16, 0x00, 0x20, 0x02, 0x20, 0x06, 0x00, 0x21, 0x02, 0x01, 0x05, 0x00, 0x0C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x02, 0x00, 0x78, 0x00, 0x08, 0x0F, 0x38, 0x36, 0x31, 0x34, 0x31, 0x30, 0x30, 0x34, 0x37, 0x36, 0x38, 0x39, 0x36, 0x30, 0x38, 0x00, 0x09, 0x0F, 0x34, 0x36, 0x30, 0x30, 0x31, 0x33, 0x31, 0x30, 0x37, 0x37, 0x30, 0x37, 0x31, 0x35, 0x35, 0x00, 0x19, 0x00, 0x00, 0x07, 0x02, 0x00, 0x14, 0x00, 0x0A, 0x04, 0x63, 0x74, 0x6E, 0x62, 0x00, 0x04, 0x0E, 0x31, 0x30, 0x2E, 0x32, 0x31, 0x32, 0x2E, 0x31, 0x34, 0x31, 0x2E, 0x31, 0x30, 0x35, 0x00, 0x05, 0x04, 0x7A, 0x70, 0xFF, 0xC7, 0x00, 0x06, 0x02, 0x07, 0x5B, 0x00, 0x2E, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2F, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x33, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x35, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x59
            };

            byte[] xjsj = new byte[] {
                0x95, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0xE4, 0x06, 0x0B, 0x04, 0x0B, 0x04, 0x16, 0x0E, 0x00, 0x03, 0x00, 0x00, 0x3B, 0x00, 0x0D, 0x01, 0xFF, 0x00, 0x0F, 0x02, 0x00, 0xD5, 0x00, 0x0E, 0x02, 0x01, 0xFC, 0x00, 0x10, 0x02, 0x00, 0x6A, 0x00, 0x1E, 0x02, 0x00, 0x00, 0x00, 0x13, 0x04, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x12, 0x04, 0x00, 0x00, 0x1A, 0xF4, 0x00, 0x1B, 0x02, 0x00, 0x1E, 0x00, 0x15, 0x02, 0x00, 0xF0, 0x00, 0x02, 0x01, 0x12, 0x00, 0x28, 0x04, 0x00, 0x00, 0x00, 0x04, 0x52, 0x01, 0x59
            };

            try
            {
                if (topic.Contains("cmdstr"))
                {
                    string payloadstr = Encoding.UTF8.GetString(payload);
                    string padleftStr = string.Empty;
                    payloadstr = payloadstr.Replace("\0", string.Empty);
                    logger.Info(payloadstr);
                    payload = HexFormatHelper.StringConvertHexBytes(payloadstr);
                }
                var uploadOrigin = NBReceivedHelper.AnalyzeMessage(payload);
                if (uploadOrigin != null)
                {
                    if (uploadOrigin.commandCode == 0x04 || uploadOrigin.commandCode == 0x0E)//参数设置和数据上报
                    {
                        uploadOrigin.uploadEntitys = new Dictionary <byte, Dictionary <byte, UploadEntity> >();
                        NBReceivedHelper.GetUploadEntity(uploadOrigin.uploadEntitys, uploadOrigin.data, 0);
                        await UploadContext.GetInstance().GetUploadSchedule().Run(uploadOrigin);
                    }
                }
            }
            catch (Exception ex)
            {
                var strPayLoad = HexFormatHelper.HexBytesConvertString(payload);
                logger.Error(this.GetType().FullName + " Topic:" + topic + ",Payload:" + strPayLoad + ex.ToString());
                UPLogger.Show(ex.Message);
                //throw;
            }
        }