Ejemplo n.º 1
0
        internal async Task ProcessFile(Guid uploadHistoryKey)
        {
            UploadHistory history = null;

            try
            {
                history = await historyStore.Find(uploadHistoryKey);

                var processor = ProcessorResolver.Resolve(history.Filename);
                if (processor == null)
                {
                    throw new Exception("The file format is not supported.");
                }

                var data = await historyStore.FindFileData(uploadHistoryKey);

                var parseResult = processor.Parse(data);
                var movies      = parseResult.Movies.Where(e => e.FilmingLocations.Any());

                foreach (var movie in movies)
                {
                    foreach (var location in movie.FilmingLocations)
                    {
                        var geocode = await geocodingService.Geocode(location.FormattedAddress);

                        if (geocode == null)
                        {
                            continue;
                        }

                        location.AddressKey       = geocode.Key;
                        location.FormattedAddress = geocode.FormattedAddress;
                        location.Latitude         = geocode.Latitude;
                        location.Longitude        = geocode.Longitude;
                    }

                    // Remove locations, that geocode service couldn't resolve.
                    movie.FilmingLocations = movie.FilmingLocations.Where(e => e.AddressKey != default(Guid)).ToList();

                    await movieService.Merge(movie);
                }

                history.Status = UploadStatus.Done;
                history.Errors = parseResult.Errors.ToArray();
                await historyStore.Update(history);
            }
            catch (Exception ex)
            {
                if (history != null)
                {
                    history.Status = UploadStatus.Done;
                    history.Errors = new[] { ex.Message };
                    await historyStore.Update(history);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <UploadHistory> Create(UploadHistory history, byte[] fileData)
        {
            db.Insert(history);
            db.Insert(new FileData {
                Key = history.Key, Data = fileData
            });
            await db.Commit();

            return(history);
        }
Ejemplo n.º 3
0
        public async Task <OperationResult <UploadHistory> > ScheduleForProcessing(Abstraction.Service.File file)
        {
            var processor = ProcessorResolver.Resolve(file.Filename);

            if (processor == null)
            {
                return(OperationResult <UploadHistory> .Failed("The file format is not supported."));
            }

            var history = new UploadHistory
            {
                Key       = Guid.NewGuid(),
                Filename  = file.Filename,
                Status    = UploadStatus.Pending,
                Timestamp = DateTimeOffset.UtcNow,
                Errors    = new string[0]
            };
            await historyStore.Create(history, file.Data);

            backgroundJobClient.Enqueue <FileJob>(job => job.ProcessFile(history.Key));

            return(OperationResult <UploadHistory> .Success(history));
        }
Ejemplo n.º 4
0
        public async Task <IHttpActionResult> UploadFile(string filename, string mimeType, string filesize, int userid)
        {
            string       root   = HttpContext.Current.Server.MapPath("~/App_Data/");
            BinaryWriter Writer = null;

            try
            {
                // Authenticate
                // return BadRequest("403");

                // Validations
                // file size
                if (Int32.Parse(filesize) > 1000000)
                {
                    return(BadRequest("400"));
                }
                // file exists
                if (File.Exists(root + filename))
                {
                    return(BadRequest("400"));
                }

                // Read the form data.
                string strFile = await Request.Content.ReadAsStringAsync();

                // Save File
                Byte[] bytes = Convert.FromBase64String(strFile.Substring(strFile.IndexOf(",") + 1));
                Writer = new BinaryWriter(File.OpenWrite(root + filename));

                // Writer raw data
                Writer.Write(bytes);
                Writer.Flush();
                Writer.Close();

                // Add entry in database
                FileList FileList = new FileList();
                FileList.filename         = filename;
                FileList.filesize         = filesize;
                FileList.fileUser         = userid;
                FileList.mimeType         = mimeType;
                FileList.creationdDate    = DateTime.Now;
                FileList.modificationDate = DateTime.Now;

                db.FileLists.Add(FileList);

                // Add entry in UploadHistory
                UploadHistory log = new UploadHistory();
                log.userid          = userid;
                log.uploadTimestamp = DateTime.Now;
                log.filename        = filename;
                db.UploadHistories.Add(log);

                db.SaveChanges();
            }
            catch (System.Exception e)
            {
                Writer.Flush();
                Writer.Close();

                return(BadRequest("500"));
            }

            return(Ok("204"));
        }
Ejemplo n.º 5
0
        public async Task <UploadHistory> Update(UploadHistory history)
        {
            await db.UpdateAndCommit(history);

            return(history);
        }