Ejemplo n.º 1
0
 public void BackgroundTaskQueueTest_DequeueAsync()
 {
     _bgTaskQueue.QueueBackgroundWorkItem(async token =>
     {
         for (int delayLoop = 0; delayLoop < 3; delayLoop++)
         {
             await Task.Delay(TimeSpan.FromSeconds(1), token);
             Console.WriteLine(delayLoop);
             // Cancel request > not tested very good
             await _bgTaskQueue.DequeueAsync(token);
         }
     });
     Assert.IsNotNull(_bgTaskQueue);
 }
Ejemplo n.º 2
0
        [ProducesResponseType(typeof(List <ImportIndexItem>), 415)] // Wrong input (e.g. wrong extenstion type)
        public async Task <IActionResult> IndexPost()
        {
            var tempImportPaths = await Request.StreamFile(_appSettings, _selectorStorage);

            var importSettings = new ImportSettingsModel(Request);

            var fileIndexResultsList = await _import.Preflight(tempImportPaths, importSettings);

            // Import files >
            _bgTaskQueue.QueueBackgroundWorkItem(async token =>
            {
                await ImportPostBackgroundTask(tempImportPaths, importSettings, _appSettings.IsVerbose());
            });

            // When all items are already imported
            if (importSettings.IndexMode &&
                fileIndexResultsList.All(p => p.Status != ImportStatus.Ok))
            {
                Response.StatusCode = 206;
            }

            // Wrong input (extension is not allowed)
            if (fileIndexResultsList.All(p => p.Status == ImportStatus.FileError))
            {
                Response.StatusCode = 415;
            }

            return(Json(fileIndexResultsList));
        }
Ejemplo n.º 3
0
        public async Task <FileIndexItem.ExifStatus> ManualSync(string subPath,
                                                                string operationId = null)
        {
            var fileIndexItem = await _query.GetObjectByFilePathAsync(subPath);

            // on a new database ->
            if (subPath == "/" && fileIndexItem == null)
            {
                fileIndexItem = new FileIndexItem();
            }
            if (fileIndexItem == null)
            {
                _logger.LogInformation($"[ManualSync] NotFoundNotInIndex skip for: {subPath}");
                return(FileIndexItem.ExifStatus.NotFoundNotInIndex);
            }

            if (_cache.TryGetValue(ManualSyncCacheName + subPath, out _))
            {
                // also used in removeCache
                _query.RemoveCacheParentItem(subPath);
                _logger.LogInformation($"[ManualSync] Cache hit skip for: {subPath}");
                return(FileIndexItem.ExifStatus.OperationNotSupported);
            }

            _cache.Set(ManualSyncCacheName + subPath, true,
                       new TimeSpan(0, 1, 0));

            _bgTaskQueue.QueueBackgroundWorkItem(async _ =>
            {
                await BackgroundTask(fileIndexItem.FilePath, operationId);
            });

            return(FileIndexItem.ExifStatus.Ok);
        }
Ejemplo n.º 4
0
        [ProducesResponseType(typeof(string), 200)]        // "Not found"
        public IActionResult GeoSyncFolder(
            string f   = "/",
            bool index = true,
            bool overwriteLocationNames = false
            )
        {
            if (_iStorage.IsFolderOrFile(f) == FolderOrFileModel.FolderOrFileTypeList.Deleted)
            {
                return(NotFound("Folder location is not found"));
            }

            var operationId = HttpContext.GetOperationId();

            _bgTaskQueue.QueueBackgroundWorkItem(async token =>
            {
                _logger.LogInformation($"{nameof(GeoSyncFolder)} started {f} {DateTime.UtcNow.ToShortTimeString()}");
                var operationHolder = RequestTelemetryHelper.GetOperationHolder(_serviceScopeFactory,
                                                                                nameof(GeoSyncFolder), operationId);

                var geoBackgroundTask = _serviceScopeFactory.CreateScope().ServiceProvider
                                        .GetService <IGeoBackgroundTask>();
                var result = await geoBackgroundTask.GeoBackgroundTaskAsync(f, index,
                                                                            overwriteLocationNames);

                operationHolder.SetData(_serviceScopeFactory, result);

                _logger.LogInformation($"{nameof(GeoSyncFolder)} end {f} {operationHolder.Telemetry?.Duration}");
            });

            return(Json("job started"));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> UpdateAsync(FileIndexItem inputModel, string f, bool append,
                                                      bool collections = true, int rotateClock = 0)
        {
            var stopwatch = StopWatchLogger.StartUpdateReplaceStopWatch();

            var inputFilePaths = PathHelper.SplitInputFilePaths(f);

            var(fileIndexResultsList, changedFileIndexItemName) = await _metaPreflight.Preflight(inputModel,
                                                                                                 inputFilePaths, append, collections, rotateClock);

            var operationId = HttpContext.GetOperationId();

            // Update >
            _bgTaskQueue.QueueBackgroundWorkItem(async _ =>
            {
                var operationHolder = RequestTelemetryHelper.GetOperationHolder(_scopeFactory,
                                                                                nameof(UpdateAsync), operationId);

                var metaUpdateService = _scopeFactory.CreateScope()
                                        .ServiceProvider.GetRequiredService <IMetaUpdateService>();

                var data = await metaUpdateService.UpdateAsync(
                    changedFileIndexItemName, fileIndexResultsList, null,
                    collections, append, rotateClock);
                operationHolder.SetData(_scopeFactory, data);
            });

            // before sending not founds
            new StopWatchLogger(_logger).StopUpdateReplaceStopWatch("update", f, collections, stopwatch);

            // When all items are not found
            if (fileIndexResultsList.All(p => p.Status != FileIndexItem.ExifStatus.Ok &&
                                         p.Status != FileIndexItem.ExifStatus.Deleted))
            {
                return(NotFound(fileIndexResultsList));
            }

            // Clone an new item in the list to display
            var returnNewResultList = fileIndexResultsList.Select(item => item.Clone()).ToList();

            // when switching very fast between images the background task has not run yet
            _metaUpdateService.UpdateReadMetaCache(returnNewResultList);

            // Push direct to socket when update or replace to avoid undo after a second
            _logger.LogInformation($"[UpdateController] send to socket {f}");

            await Task.Run(async() => await TaskRun(fileIndexResultsList));

            return(Json(returnNewResultList));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Replace(string f, string fieldName, string search,
                                                  string replace, bool collections = true)
        {
            var stopwatch = StopWatchLogger.StartUpdateReplaceStopWatch();

            var fileIndexResultsList = await _metaReplaceService
                                       .Replace(f, fieldName, search, replace, collections);

            var resultsOkOrDeleteList = fileIndexResultsList.Where(
                p => p.Status == FileIndexItem.ExifStatus.Ok ||
                p.Status == FileIndexItem.ExifStatus.Deleted).ToList();

            var changedFileIndexItemName = resultsOkOrDeleteList.
                                           ToDictionary(item => item.FilePath, item => new List <string> {
                fieldName
            });

            // Update >
            _bgTaskQueue.QueueBackgroundWorkItem(async _ =>
            {
                var metaUpdateService = _scopeFactory.CreateScope()
                                        .ServiceProvider.GetRequiredService <IMetaUpdateService>();
                await metaUpdateService
                .UpdateAsync(changedFileIndexItemName, resultsOkOrDeleteList,
                             null, collections, false, 0);
            });

            // before sending not founds
            new StopWatchLogger(_logger).StopUpdateReplaceStopWatch("update", f, collections, stopwatch);

            // When all items are not found
            if (!resultsOkOrDeleteList.Any())
            {
                return(NotFound(fileIndexResultsList));
            }

            // Push direct to socket when update or replace to avoid undo after a second
            var webSocketResponse =
                new ApiNotificationResponseModel <List <FileIndexItem> >(resultsOkOrDeleteList, ApiNotificationType.Replace);
            await _connectionsService.NotificationToAllAsync(webSocketResponse, CancellationToken.None);

            return(Json(fileIndexResultsList));
        }
Ejemplo n.º 7
0
        public IActionResult PublishCreate(string f, string itemName,
                                           string publishProfileName, bool force = false)
        {
            _webLogger.LogInformation($"[/api/publish/create] Press publish: {itemName} {f} {DateTime.UtcNow}");
            var inputFilePaths = PathHelper.SplitInputFilePaths(f).ToList();
            var info           = _metaInfo.GetInfo(inputFilePaths, false);

            if (info.All(p => p.Status != FileIndexItem.ExifStatus.Ok && p.Status != FileIndexItem.ExifStatus.ReadOnly))
            {
                return(NotFound(info));
            }

            var slugItemName = _appSettings.GenerateSlug(itemName, true);
            var location     = Path.Combine(_appSettings.TempFolder, slugItemName);

            if (CheckIfNameExist(slugItemName))
            {
                if (!force)
                {
                    return(Conflict($"name {slugItemName} exist"));
                }
                ForceCleanPublishFolderAndZip(location);
            }

            // todo: check if overlay image path: WebHtmlPublish/EmbeddedViews/default.png or something else exists

            // Creating Publish is a background task
            _bgTaskQueue.QueueBackgroundWorkItem(async token =>
            {
                var renderCopyResult = await _publishService.RenderCopy(info,
                                                                        publishProfileName, itemName, location);
                await _publishService.GenerateZip(_appSettings.TempFolder, itemName,
                                                  renderCopyResult, true);
                _webLogger.LogInformation($"[/api/publish/create] done: {itemName} {DateTime.UtcNow}");
            });

            // Get the zip  by	[HttpGet("/export/zip/{f}.zip")]
            return(Json(slugItemName));
        }
Ejemplo n.º 8
0
        public IActionResult CreateZip(string f, bool collections = true, bool thumbnail = false)
        {
            var inputFilePaths = PathHelper.SplitInputFilePaths(f);

            var(zipOutputName, fileIndexResultsList) = _export.Preflight(inputFilePaths, collections, thumbnail);

            // When all items are not found
            // allow read only
            if (fileIndexResultsList.All(p => p.Status != FileIndexItem.ExifStatus.Ok))
            {
                return(NotFound(fileIndexResultsList));
            }

            // NOT covered: when try to export for example image thumbnails of xml file

            // Creating a zip is a background task
            _bgTaskQueue.QueueBackgroundWorkItem(async token =>
            {
                await _export.CreateZip(fileIndexResultsList, thumbnail, zipOutputName);
            });

            // for the rest api
            return(Json(zipOutputName));
        }