private void CopyFile(string path, string newPath)
        {
            path = GetRelativePath(path);

            var file = _fileSystem.GetFile(path);

            if (!file.Exists)
            {
                throw new Exception(LangRes("E_CopyFileInvalisPath"));
            }

            try
            {
                newPath = _fileSystem.Combine(GetRelativePath(newPath), file.Name);
                if (_fileSystem.CheckFileUniqueness(newPath, out var newFile))
                {
                    newPath = newFile.Path;
                }

                _fileSystem.CopyFile(path, newPath);

                Response.Write(GetResultString());
            }
            catch (Exception ex)
            {
                throw new Exception(LangRes("E_CopyFile") + ": " + ex.Message);
            }
        }
        /// <summary>
        /// After a content has been copied, also copy uploaded files.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="args">The event arguments.</param>
        public void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs <IContent> args)
        {
            // get the image cropper field properties
            var properties = args.Original.Properties.Where(IsCropperField);

            // copy files
            var isUpdated = false;

            foreach (var property in properties)
            {
                //copy each of the property values (variants, segments) to the destination by using the edited value
                foreach (var propertyValue in property.Values)
                {
                    var propVal = property.GetValue(propertyValue.Culture, propertyValue.Segment);
                    var src     = GetFileSrcFromPropertyValue(propVal, out var jo);
                    if (src == null)
                    {
                        continue;
                    }
                    var sourcePath = _mediaFileSystem.GetRelativePath(src);
                    var copyPath   = _mediaFileSystem.CopyFile(args.Copy, property.PropertyType, sourcePath);
                    jo["src"] = _mediaFileSystem.GetUrl(copyPath);
                    args.Copy.SetValue(property.Alias, jo.ToString(), propertyValue.Culture, propertyValue.Segment);
                    isUpdated = true;
                }
            }
            // if updated, re-save the copy with the updated value
            if (isUpdated)
            {
                sender.Save(args.Copy);
            }
        }
Пример #3
0
        private void CopyFile(string path, string newPath)
        {
            path = GetRelativePath(path);

            var file = _fileSystem.GetFile(path);

            if (!file.Exists)
            {
                throw new FileNotFoundException($"File {path} does not exist.", path);
            }

            try
            {
                newPath = _fileSystem.Combine(GetRelativePath(newPath), file.Name);

                if (_fileSystem.CheckUniqueFileName(newPath, out var uniquePath))
                {
                    newPath = uniquePath;
                }

                _fileSystem.CopyFile(path, newPath, false);
                Response.Write(GetResultString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// After a content has been copied, also copy uploaded files.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="args">The event arguments.</param>
        internal void ContentServiceCopied(IContentService sender, Core.Events.CopyEventArgs <IContent> args)
        {
            // get the upload field properties with a value
            var properties = args.Original.Properties.Where(IsUploadField);

            // copy files
            var isUpdated = false;

            foreach (var property in properties)
            {
                //copy each of the property values (variants, segments) to the destination
                foreach (var propertyValue in property.Values)
                {
                    var propVal = property.GetValue(propertyValue.Culture, propertyValue.Segment);
                    if (propVal == null || !(propVal is string str) || str.IsNullOrWhiteSpace())
                    {
                        continue;
                    }
                    var sourcePath = _mediaFileSystem.GetRelativePath(str);
                    var copyPath   = _mediaFileSystem.CopyFile(args.Copy, property.PropertyType, sourcePath);
                    args.Copy.SetValue(property.Alias, _mediaFileSystem.GetUrl(copyPath), propertyValue.Culture, propertyValue.Segment);
                    isUpdated = true;
                }
            }

            // if updated, re-save the copy with the updated value
            if (isUpdated)
            {
                sender.Save(args.Copy);
            }
        }
        private void CopyFile(string path, string newPath)
        {
            path    = GetRelativePath(path);
            newPath = GetRelativePath(newPath);

            if (!_fileSystem.FileExists(path))
            {
                throw new Exception(LangRes("E_CopyFileInvalisPath"));
            }

            try
            {
                var file    = _fileSystem.GetFile(path);
                var newName = GetUniqueFileName(newPath, file.Name);

                _fileSystem.CopyFile(path, _fileSystem.Combine(newPath, newName));

                Response.Write(GetResultString());
            }
            catch
            {
                throw new Exception(LangRes("E_CopyFile"));
            }
        }
Пример #6
0
        private void UpdateMediaFile(IMedia media, bool obfuscate)
        {
            if (!media.Properties.Contains(Constants.Conventions.Media.File))
            {
                // not a media file, so ignore
                return;
            }

            var file = media.Properties[Constants.Conventions.Media.File];

            var fileSrc = file.GetValue()?.ToString();

            if (fileSrc.DetectIsJson())
            {
                fileSrc = JsonConvert.DeserializeObject <JObject>(fileSrc).GetValue("src")?.ToString();
            }

            var fileDirectory = Path.GetDirectoryName(fileSrc);
            var fileName      = Path.GetFileName(fileSrc);

            if (obfuscate && fileName.StartsWith(deletedMediaPrefix))
            {
                // already renamed, nothing to do here
                return;
            }
            else if (!obfuscate && !fileName.StartsWith(deletedMediaPrefix))
            {
                // doesn't have the prefix, nothing to do here
                return;
            }

            var oldFilePath = Path.Combine(fileDirectory, fileName);
            var newFilePath = Path.Combine(fileDirectory, (obfuscate ? deletedMediaPrefix + fileName : fileName.TrimStart(deletedMediaPrefix)));

            _mediaFileSystem.CopyFile(oldFilePath, newFilePath);
            _mediaFileSystem.DeleteFile(oldFilePath);

            file.SetValue(newFilePath);

            _mediaService.Save(media, raiseEvents: false);
        }