Example #1
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));
        }
Example #2
0
        public async Task <IActionResult> UploadToFolder()
        {
            var to = Request.Headers["to"].ToString();

            if (string.IsNullOrWhiteSpace(to))
            {
                return(BadRequest("missing 'to' header"));
            }

            var parentDirectory = GetParentDirectoryFromRequestHeader();

            if (parentDirectory == null)
            {
                return(NotFound(new ImportIndexItem {
                    Status = ImportStatus.ParentDirectoryNotFound
                }));
            }

            var tempImportPaths = await Request.StreamFile(_appSettings, _selectorStorage);

            var fileIndexResultsList = await _import.Preflight(tempImportPaths,
                                                               new ImportSettingsModel { IndexMode = false });

            for (var i = 0; i < fileIndexResultsList.Count; i++)
            {
                if (fileIndexResultsList[i].Status != ImportStatus.Ok)
                {
                    continue;
                }

                var tempFileStream = _iHostStorage.ReadStream(tempImportPaths[i]);
                var fileName       = Path.GetFileName(tempImportPaths[i]);

                // subPath is always unix style
                var subPath = PathHelper.AddSlash(parentDirectory) + fileName;
                if (parentDirectory == "/")
                {
                    subPath = parentDirectory + fileName;
                }

                // to get the output in the result right
                fileIndexResultsList[i].FileIndexItem.FileName        = fileName;
                fileIndexResultsList[i].FileIndexItem.ParentDirectory = parentDirectory;
                fileIndexResultsList[i].FilePath = subPath;
                // Do sync action before writing it down
                fileIndexResultsList[i].FileIndexItem = await SyncItem(fileIndexResultsList[i].FileIndexItem);

                var writeStatus =
                    await _iStorage.WriteStreamAsync(tempFileStream, subPath + ".tmp");

                await tempFileStream.DisposeAsync();

                // to avoid partly written stream to be read by an other application
                _iStorage.FileDelete(subPath);
                _iStorage.FileMove(subPath + ".tmp", subPath);
                _logger.LogInformation($"write {subPath} is {writeStatus}");

                // clear directory cache
                _query.RemoveCacheParentItem(subPath);

                var deleteStatus = _iHostStorage.FileDelete(tempImportPaths[i]);
                _logger.LogInformation($"[UploadController] delete {tempImportPaths[i]} is {deleteStatus}");

                var parentPath = Directory.GetParent(tempImportPaths[i])?.FullName;
                if (!string.IsNullOrEmpty(parentPath) && parentPath != _appSettings.TempFolder)
                {
                    _iHostStorage.FolderDelete(parentPath);
                }

                await _metaExifThumbnailService.AddMetaThumbnail(subPath,
                                                                 fileIndexResultsList[i].FileIndexItem.FileHash);
            }

            // send all uploads as list
            var socketResult = fileIndexResultsList
                               .Where(p => p.Status == ImportStatus.Ok)
                               .Select(item => item.FileIndexItem).ToList();

            var webSocketResponse = new ApiNotificationResponseModel <List <FileIndexItem> >(
                socketResult, ApiNotificationType.UploadFile);
            await _connectionsService.NotificationToAllAsync(webSocketResponse, CancellationToken.None);

            // Wrong input (extension is not allowed)
            if (fileIndexResultsList.All(p => p.Status == ImportStatus.FileError))
            {
                _logger.LogInformation($"Wrong input extension is not allowed" +
                                       $" {string.Join(",",fileIndexResultsList.Select(p => p.FilePath))}");
                Response.StatusCode = 415;
            }

            return(Json(fileIndexResultsList));
        }