Ejemplo n.º 1
0
        public async Task <IActionResult> MoveMedia(string oldPath, string newPath)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageOwnMedia))
            {
                return(Unauthorized());
            }

            if (string.IsNullOrEmpty(oldPath) || string.IsNullOrEmpty(newPath))
            {
                return(NotFound());
            }

            if (await _mediaFileStore.GetFileInfoAsync(oldPath) == null)
            {
                return(NotFound());
            }

            if (await _mediaFileStore.GetFileInfoAsync(newPath) != null)
            {
                return(StatusCode(StatusCodes.Status403Forbidden, T["Cannot move media because a file already exists with the same name"]));
            }

            await _mediaFileStore.MoveFileAsync(oldPath, newPath);

            return(Ok());
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> MoveMedia(string oldPath, string newPath)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageMedia) ||
                !await _authorizationService.AuthorizeAsync(User, Permissions.ManageAttachedMediaFieldsFolder, (object)oldPath))
            {
                return(Forbid());
            }

            if (string.IsNullOrEmpty(oldPath) || string.IsNullOrEmpty(newPath))
            {
                return(NotFound());
            }

            if (await _mediaFileStore.GetFileInfoAsync(oldPath) == null)
            {
                return(NotFound());
            }

            if (!_allowedFileExtensions.Contains(Path.GetExtension(newPath), StringComparer.OrdinalIgnoreCase))
            {
                return(StatusCode(StatusCodes.Status403Forbidden, S["This file extension is not allowed: {0}", Path.GetExtension(newPath)]));
            }

            if (await _mediaFileStore.GetFileInfoAsync(newPath) != null)
            {
                return(StatusCode(StatusCodes.Status403Forbidden, S["Cannot move media because a file already exists with the same name"]));
            }

            await _mediaFileStore.MoveFileAsync(oldPath, newPath);

            return(Ok());
        }
        // Newly added files
        private async Task MoveNewFilesToContentItemDirAndUpdatePathsAsync(List <EditMediaFieldItemInfo> items, ContentItem contentItem)
        {
            foreach (var item in items.Where(i => !i.IsRemoved && !String.IsNullOrEmpty(i.Path)))
            {
                var fileInfo = await _fileStore.GetFileInfoAsync(item.Path);

                if (fileInfo == null)
                {
                    _logger.LogError("A file with the path '{Path}' does not exist.", item.Path);
                    return;
                }

                var targetDir     = GetContentItemFolder(contentItem);
                var finalFileName = (await GetFileHashAsync(item.Path)) + GetFileExtension(item.Path);
                var finalFilePath = _fileStore.Combine(targetDir, finalFileName);

                await _fileStore.TryCreateDirectoryAsync(targetDir);

                // When there is a validation error before creating the content item we can end up with an empty folder
                // because the content item is different on each form submit . We need to remove that empty folder.
                var previousDirPath = fileInfo.DirectoryPath;

                // fileName is a hash of the file. We preserve disk space by reusing the file.
                if (await _fileStore.GetFileInfoAsync(finalFilePath) == null)
                {
                    await _fileStore.MoveFileAsync(item.Path, finalFilePath);
                }

                item.Path = finalFilePath;

                await DeleteDirIfEmptyAsync(previousDirPath);
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> MoveMediaList(string[] mediaNames, string sourceFolder, string targetFolder)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageMedia) ||
                !await _authorizationService.AuthorizeAsync(User, Permissions.ManageAttachedMediaFieldsFolder, (object)sourceFolder) ||
                !await _authorizationService.AuthorizeAsync(User, Permissions.ManageAttachedMediaFieldsFolder, (object)targetFolder))
            {
                return(Unauthorized());
            }

            if ((mediaNames == null) || (mediaNames.Length < 1) ||
                string.IsNullOrEmpty(sourceFolder) ||
                string.IsNullOrEmpty(targetFolder))
            {
                return(NotFound());
            }

            sourceFolder = sourceFolder == "root" ? string.Empty : sourceFolder;
            targetFolder = targetFolder == "root" ? string.Empty : targetFolder;

            var filesOnError = new List <string>();

            foreach (var name in mediaNames)
            {
                var sourcePath = _mediaFileStore.Combine(sourceFolder, name);
                var targetPath = _mediaFileStore.Combine(targetFolder, name);
                try
                {
                    await _mediaFileStore.MoveFileAsync(sourcePath, targetPath);
                }
                catch (FileStoreException)
                {
                    filesOnError.Add(sourcePath);
                }
            }

            if (filesOnError.Count > 0)
            {
                return(BadRequest(T["Error when moving files. Maybe they already exist on the target folder? Files on error: {0}", string.Join(",", filesOnError)].ToString()));
            }
            else
            {
                return(Ok());
            }
        }