Exemplo n.º 1
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));
        }
Exemplo n.º 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));
        }