public async Task ReceiveAsync(MediaMoverContext context, MediaFile mediaFile, Stream stream)
        {
            Guard.NotNull(context, nameof(context));
            Guard.NotNull(mediaFile, nameof(mediaFile));

            // store data into file
            if (stream != null && stream.Length > 0)
            {
                var filePath = GetPath(mediaFile);

                if (!_fileSystem.FileExists(filePath))
                {
                    // TBD: (mc) We only save the file if it doesn't exist yet.
                    // This should save time and bandwidth in the case where the target
                    // is a cloud based file system (like Azure BLOB).
                    // In such a scenario it'd be advisable to copy the files manually
                    // with other - maybe more performant - tools before performing the provider switch.

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

                    using (stream)
                    {
                        await _fileSystem.SaveStreamAsync(filePath, stream);
                    }

                    context.AffectedFiles.Add(filePath);
                }
            }
        }
Пример #2
0
 public virtual async Task PutAsync(CachedImage cachedImage, Stream stream)
 {
     if (await PreparePut(cachedImage, stream))
     {
         var path = BuildPath(cachedImage.Path);
         await _fileSystem.SaveStreamAsync(path, stream);
         await PostPut(cachedImage, path);
     }
 }
Пример #3
0
        public async Task PutAsync(CachedImage cachedImage, Stream stream)
        {
            if (PreparePut(cachedImage, stream))
            {
                var path = BuildPath(cachedImage.Path);
                await _fileSystem.SaveStreamAsync(path, stream);

                cachedImage.Exists = true;
                cachedImage.File   = _fileSystem.GetFile(path);
            }
        }
        private async Task UploadAsync(string path, bool external = false)
        {
            path = GetRelativePath(path);

            string message  = null;
            var    hasError = false;
            var    width    = 0;
            var    height   = 0;
            string url      = null;

            int.TryParse(GetSetting("MAX_IMAGE_WIDTH"), out width);
            int.TryParse(GetSetting("MAX_IMAGE_HEIGHT"), out height);

            var tempDir = FileSystemHelper.TempDirTenant("roxy " + CommonHelper.GenerateRandomInteger().ToString());

            try
            {
                var notify = Request.Files.Count < 4;

                // copy uploaded files to temp folder and resize them
                for (var i = 0; i < Request.Files.Count; ++i)
                {
                    var file      = Request.Files[i];
                    var extension = Path.GetExtension(file.FileName);

                    if (IsAllowedFileType(extension))
                    {
                        var dest = Path.Combine(tempDir, file.FileName);
                        file.SaveAs(dest);

                        if (GetFileContentType(extension) == "image" && extension != ".svg")
                        {
                            ImageResize(dest, dest, width, height, notify);
                        }
                    }
                    else
                    {
                        message = LangRes("E_UploadNotAll");
                    }
                }

                // Copy files to file storage
                foreach (var tempPath in Directory.EnumerateFiles(tempDir, "*", SearchOption.TopDirectoryOnly))
                {
                    using (var stream = new FileStream(tempPath, FileMode.Open, FileAccess.Read))
                    {
                        var name    = Path.GetFileName(tempPath);
                        var newPath = _fileSystem.Combine(path, name);
                        if (_fileSystem.CheckFileUniqueness(newPath, out var file))
                        {
                            newPath = file.Path;
                        }

                        await _fileSystem.SaveStreamAsync(newPath, stream);

                        url = _fileSystem.GetPublicUrl(newPath);
                    }
                }
            }
            catch (Exception ex)
            {
                hasError = true;
                message  = ex.Message;
            }
            finally
            {
                FileSystemHelper.ClearDirectory(tempDir, true);
            }

            if (IsAjaxUpload())
            {
                if (external)
                {
                    var result = new
                    {
                        Success = !hasError,
                        Url     = url,
                        Message = message
                    };
                    Response.ContentType = "text/json";
                    Response.Write(JsonConvert.SerializeObject(result));
                }
                else
                {
                    if (message.HasValue())
                    {
                        Response.Write(GetResultString(message, hasError ? "error" : "ok"));
                    }
                }
            }
            else
            {
                Response.Write("<script>");
                Response.Write("parent.fileUploaded(" + GetResultString(message, hasError ? "error" : "ok") + ");");
                Response.Write("</script>");
            }
        }
Пример #5
0
        private async Task UploadAsync(string destinationPath, bool external = false)
        {
            destinationPath = GetRelativePath(destinationPath);

            string message  = null;
            var    hasError = false;

            try
            {
                // Copy uploaded files to file storage
                for (var i = 0; i < Request.Files.Count; ++i)
                {
                    var uploadedFile = Request.Files[i];
                    var extension    = Path.GetExtension(uploadedFile.FileName);

                    if (IsAllowedFileType(extension))
                    {
                        var path = _fileSystem.Combine(destinationPath, uploadedFile.FileName);
                        if (_fileSystem.CheckUniqueFileName(path, out var uniquePath))
                        {
                            path = uniquePath;
                        }

                        await _fileSystem.SaveStreamAsync(path, uploadedFile.InputStream);
                    }
                    else
                    {
                        message = LangRes("E_UploadNotAll");
                    }
                }
            }
            catch (Exception ex)
            {
                hasError = true;
                message  = ex.Message;
            }

            if (IsAjaxUpload())
            {
                if (external)
                {
                    var result = new
                    {
                        Success = !hasError,
                        Message = message
                    };
                    Response.ContentType = "text/json";
                    Response.Write(JsonConvert.SerializeObject(result));
                }
                else
                {
                    if (message.HasValue())
                    {
                        Response.Write(GetResultString(message, hasError ? "error" : "ok"));
                    }
                }
            }
            else
            {
                Response.Write("<script>");
                Response.Write("parent.fileUploaded(" + GetResultString(message, hasError ? "error" : "ok") + ");");
                Response.Write("</script>");
            }
        }