internal static string AppendIndexerToFilePath(string parentDirectory, string fileName, int index) { if (index >= 1) { fileName = string.Concat( FilenamesHelper.GetFileNameWithoutExtension(fileName), $"_{index}.", FilenamesHelper.GetFileExtensionWithoutDot(fileName) ); } return(PathHelper.AddSlash(parentDirectory) + PathHelper.RemovePrefixDbSlash(fileName)); }
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)); }
public async Task <IActionResult> UploadToFolderSidecarFile() { var to = Request.Headers["to"].ToString(); if (string.IsNullOrWhiteSpace(to)) { return(BadRequest("missing 'to' header")); } _logger.LogInformation($"[UploadToFolderSidecarFile] to:{to}"); var parentDirectory = GetParentDirectoryFromRequestHeader(); if (parentDirectory == null) { return(NotFound(new ImportIndexItem())); } var tempImportPaths = await Request.StreamFile(_appSettings, _selectorStorage); var importedList = new List <string>(); foreach (var tempImportSinglePath in tempImportPaths) { var data = await new PlainTextFileHelper().StreamToStringAsync( _iHostStorage.ReadStream(tempImportSinglePath)); if (!IsValidXml(data)) { continue; } var tempFileStream = _iHostStorage.ReadStream(tempImportSinglePath); var fileName = Path.GetFileName(tempImportSinglePath); var subPath = PathHelper.AddSlash(parentDirectory) + fileName; if (parentDirectory == "/") { subPath = parentDirectory + fileName; } if (_appSettings.UseDiskWatcher == false) { await new SyncSingleFile(_appSettings, _query, _iStorage, _logger).UpdateSidecarFile(subPath); } await _iStorage.WriteStreamAsync(tempFileStream, subPath); await tempFileStream.DisposeAsync(); importedList.Add(subPath); var deleteStatus = _iHostStorage.FileDelete(tempImportSinglePath); _logger.LogInformation($"delete {tempImportSinglePath} is {deleteStatus}"); } if (!importedList.Any()) { Response.StatusCode = 415; } return(Json(importedList)); }
/// <summary> /// Get the jsonSubPath `parentDir/.starsky.filename.ext.json` /// </summary> /// <param name="parentDirectory">parent Directory</param> /// <param name="fileName">and filename</param> /// <returns>parentDir/.starsky.filename.`ext.json</returns> public static string JsonLocation(string parentDirectory, string fileName) { return(PathHelper.AddSlash(parentDirectory) + ".starsky." + fileName + ".json"); }