public void Save(MediaFile mediaFile, MediaStorageItem item)
        {
            Guard.NotNull(mediaFile, nameof(mediaFile));

            // TODO: (?) if the new file extension differs from the old one then the old file never gets deleted

            var filePath = GetPath(mediaFile);

            if (item != null)
            {
                // Create folder if it does not exist yet
                var dir = Path.GetDirectoryName(filePath);
                if (!_fileSystem.FolderExists(dir))
                {
                    _fileSystem.CreateFolder(dir);
                }

                using (item)
                {
                    using var outStream = _fileSystem.GetFile(filePath).OpenWrite();
                    item.SaveTo(outStream, mediaFile);
                }
            }
            else if (_fileSystem.FileExists(filePath))
            {
                // Remove media storage if any
                _fileSystem.DeleteFile(filePath);
            }
        }
Esempio n. 2
0
        public void Save(MediaItem media, byte[] data)
        {
            Guard.NotNull(media, nameof(media));

            // TODO: (?) if the new file extension differs from the old one then the old file never gets deleted

            var filePath = GetPicturePath(media);

            if (data != null && data.LongLength != 0)
            {
                _fileSystem.WriteAllBytes(filePath, data);
            }
            else if (_fileSystem.FileExists(filePath))
            {
                _fileSystem.DeleteFile(filePath);
            }
        }
        public void Save(MediaFile mediaFile, byte[] data)
        {
            Guard.NotNull(mediaFile, nameof(mediaFile));

            // TODO: (?) if the new file extension differs from the old one then the old file never gets deleted

            var filePath = GetPath(mediaFile);

            if (data != null && data.LongLength > 0)
            {
                _fileSystem.WriteAllBytes(filePath, data);
            }
            else if (_fileSystem.FileExists(filePath))
            {
                // Remove media storage if any
                _fileSystem.DeleteFile(filePath);
            }
        }
Esempio n. 4
0
        public virtual void Delete(MediaFile picture)
        {
            var filter = string.Format("{0}*.*", picture.Id.ToString(IdFormatString));

            var files = _fileSystem.SearchFiles(_thumbsRootDir, filter);

            foreach (var file in files)
            {
                _fileSystem.DeleteFile(file);
            }
        }
Esempio n. 5
0
        private bool PrepareAddImageToCache(CachedImageResult cachedImage, byte[] buffer)
        {
            Guard.NotNull(cachedImage, nameof(cachedImage));

            if (buffer == null || buffer.Length == 0)
            {
                return(false);
            }

            if (cachedImage.Exists)
            {
                _fileSystem.DeleteFile(BuildPath(cachedImage.Path));
            }

            // create folder if needed
            string imageDir = System.IO.Path.GetDirectoryName(cachedImage.Path);

            if (imageDir.HasValue())
            {
                _fileSystem.TryCreateFolder(BuildPath(imageDir));
            }

            return(true);
        }
Esempio n. 6
0
        private bool PreparePut(CachedImage cachedImage, Stream stream)
        {
            Guard.NotNull(cachedImage, nameof(cachedImage));

            if (stream == null)
            {
                return(false);
            }

            if (cachedImage.Exists)
            {
                _fileSystem.DeleteFile(BuildPath(cachedImage.Path));
            }

            // create folder if needed
            string imageDir = System.IO.Path.GetDirectoryName(cachedImage.Path);

            if (imageDir.HasValue())
            {
                _fileSystem.CreateFolder(BuildPath(imageDir));
            }

            return(true);
        }
Esempio n. 7
0
        private void DeleteFile(string path)
        {
            path = GetRelativePath(path);
            var file = _fileSystem.GetFile(path);

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

            try
            {
                _fileSystem.DeleteFile(path);
                Response.Write(GetResultString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void DeleteFile(string path)
        {
            path = GetRelativePath(path);

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

            try
            {
                _fileSystem.DeleteFile(path);

                Response.Write(GetResultString());
            }
            catch
            {
                throw new Exception(LangRes("E_DeletеFile"));
            }
        }
Esempio n. 9
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);
        }
        private void DeleteFile(string path)
        {
            path = GetRelativePath(path);
            var file = _fileSystem.GetFile(path);

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

            try
            {
                _fileSystem.DeleteFile(path);
                _imageCache.Value.Delete(file);

                Response.Write(GetResultString());
            }
            catch (Exception ex)
            {
                throw new Exception(LangRes("E_DeletеFile") + ": " + ex.Message);
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Converts the value received from the editor into the value can be stored in the database.
        /// </summary>
        /// <param name="editorValue">The value received from the editor.</param>
        /// <param name="currentValue">The current value of the property</param>
        /// <returns>The converted value.</returns>
        /// <remarks>
        /// <para>The <paramref name="currentValue"/> is used to re-use the folder, if possible.</para>
        /// <para>editorValue.Value is used to figure out editorFile and, if it has been cleared, remove the old file - but
        /// it is editorValue.AdditionalData["files"] that is used to determine the actual file that has been uploaded.</para>
        /// </remarks>
        public override object FromEditor(ContentPropertyData editorValue, object currentValue)
        {
            // get the current path
            var currentPath = string.Empty;

            try
            {
                var svalue      = currentValue as string;
                var currentJson = string.IsNullOrWhiteSpace(svalue) ? null : JObject.Parse(svalue);
                if (currentJson != null && currentJson["src"] != null)
                {
                    currentPath = currentJson["src"].Value <string>();
                }
            }
            catch (Exception ex)
            {
                // for some reason the value is invalid so continue as if there was no value there
                _logger.Warn <ImageCropperPropertyValueEditor>(ex, "Could not parse current db value to a JObject.");
            }
            if (string.IsNullOrWhiteSpace(currentPath) == false)
            {
                currentPath = _mediaFileSystem.GetRelativePath(currentPath);
            }

            // get the new json and path
            JObject editorJson = null;
            var     editorFile = string.Empty;

            if (editorValue.Value != null)
            {
                editorJson = editorValue.Value as JObject;
                if (editorJson != null && editorJson["src"] != null)
                {
                    editorFile = editorJson["src"].Value <string>();
                }
            }

            // ensure we have the required guids
            var cuid = editorValue.ContentKey;

            if (cuid == Guid.Empty)
            {
                throw new Exception("Invalid content key.");
            }
            var puid = editorValue.PropertyTypeKey;

            if (puid == Guid.Empty)
            {
                throw new Exception("Invalid property type key.");
            }

            // editorFile is empty whenever a new file is being uploaded
            // or when the file is cleared (in which case editorJson is null)
            // else editorFile contains the unchanged value

            var uploads = editorValue.Files;

            if (uploads == null)
            {
                throw new Exception("Invalid files.");
            }
            var file = uploads.Length > 0 ? uploads[0] : null;

            if (file == null) // not uploading a file
            {
                // if editorFile is empty then either there was nothing to begin with,
                // or it has been cleared and we need to remove the file - else the
                // value is unchanged.
                if (string.IsNullOrWhiteSpace(editorFile) && string.IsNullOrWhiteSpace(currentPath) == false)
                {
                    _mediaFileSystem.DeleteFile(currentPath);
                    return(null); // clear
                }

                return(editorJson?.ToString()); // unchanged
            }

            // process the file
            var filepath = editorJson == null ? null : ProcessFile(editorValue, file, currentPath, cuid, puid);

            // remove all temp files
            foreach (var f in uploads)
            {
                File.Delete(f.TempFilePath);
            }

            // remove current file if replaced
            if (currentPath != filepath && string.IsNullOrWhiteSpace(currentPath) == false)
            {
                _mediaFileSystem.DeleteFile(currentPath);
            }

            // update json and return
            if (editorJson == null)
            {
                return(null);
            }
            editorJson["src"] = filepath == null ? string.Empty : _mediaFileSystem.GetUrl(filepath);
            return(editorJson.ToString());
        }
        /// <summary>
        /// Converts the value received from the editor into the value can be stored in the database.
        /// </summary>
        /// <param name="editorValue">The value received from the editor.</param>
        /// <param name="currentValue">The current value of the property</param>
        /// <returns>The converted value.</returns>
        /// <remarks>
        /// <para>The <paramref name="currentValue"/> is used to re-use the folder, if possible.</para>
        /// <para>The <paramref name="editorValue"/> is value passed in from the editor. We normally don't care what
        /// the editorValue.Value is set to because we are more interested in the files collection associated with it,
        /// however we do care about the value if we are clearing files. By default the editorValue.Value will just
        /// be set to the name of the file - but again, we just ignore this and deal with the file collection in
        /// editorValue.AdditionalData.ContainsKey("files")</para>
        /// <para>We only process ONE file. We understand that the current value may contain more than one file,
        /// and that more than one file may be uploaded, so we take care of them all, but we only store ONE file.
        /// Other places (FileUploadPropertyEditor...) do NOT deal with multiple files, and our logic for reusing
        /// folders would NOT work, etc.</para>
        /// </remarks>
        public override object FromEditor(ContentPropertyData editorValue, object currentValue)
        {
            var currentPath = currentValue as string;

            if (!currentPath.IsNullOrWhiteSpace())
            {
                currentPath = _mediaFileSystem.GetRelativePath(currentPath);
            }

            string editorFile = null;

            if (editorValue.Value != null)
            {
                editorFile = editorValue.Value as string;
            }

            // ensure we have the required guids
            var cuid = editorValue.ContentKey;

            if (cuid == Guid.Empty)
            {
                throw new Exception("Invalid content key.");
            }
            var puid = editorValue.PropertyTypeKey;

            if (puid == Guid.Empty)
            {
                throw new Exception("Invalid property type key.");
            }

            var uploads = editorValue.Files;

            if (uploads == null)
            {
                throw new Exception("Invalid files.");
            }
            var file = uploads.Length > 0 ? uploads[0] : null;

            if (file == null) // not uploading a file
            {
                // if editorFile is empty then either there was nothing to begin with,
                // or it has been cleared and we need to remove the file - else the
                // value is unchanged.
                if (string.IsNullOrWhiteSpace(editorFile) && string.IsNullOrWhiteSpace(currentPath) == false)
                {
                    _mediaFileSystem.DeleteFile(currentPath);
                    return(null); // clear
                }

                return(currentValue); // unchanged
            }

            // process the file
            var filepath = editorFile == null ? null : ProcessFile(editorValue, file, currentPath, cuid, puid);

            // remove all temp files
            foreach (var f in uploads)
            {
                File.Delete(f.TempFilePath);
            }

            // remove current file if replaced
            if (currentPath != filepath && string.IsNullOrWhiteSpace(currentPath) == false)
            {
                _mediaFileSystem.DeleteFile(currentPath);
            }

            // update json and return
            if (editorFile == null)
            {
                return(null);
            }
            return(filepath == null ? string.Empty : _mediaFileSystem.GetUrl(filepath));
        }