public static void SaveTo(this IHttpFile httpFile, string filePath)
 {
     using (var sw = new StreamWriter(filePath, false))
     {
         httpFile.InputStream.WriteTo(sw.BaseStream);
     }
 }
Exemple #2
0
        /// <summary>
        /// Сохраняет фото в файл на сервере
        /// </summary>
        /// <param name="photoFile">Файл, отправленный в запросе для сохранения</param>
        /// <param name="pathToSave">Название файла</param>
        public static void SavePhoto(IHttpFile photoFile, string fileName)
        {
            if (!Directory.Exists("pictures"))
            {
                Directory.CreateDirectory("pictures");
            }

            #region Проверка на пустоту

            _ = photoFile ?? throw new ArgumentNullException(nameof(photoFile));
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            #endregion

            try
            {
                using (var file = File.Create("pictures\\" + fileName))
                {
                    photoFile.WriteTo(file);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemple #3
0
 /// <summary>
 /// Add a new file.
 /// </summary>
 /// <param name="file">File to add.</param>
 public void Add(IHttpFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException("file");
     }
     _files.Add(file);
 }
Exemple #4
0
        public static async Task SaveToAsync(this IHttpFile httpFile, string filePath)
        {
#if NET6_0_OR_GREATER
            await
#endif
            using var sw = new StreamWriter(File.Create(filePath));
            await httpFile.InputStream.WriteToAsync(sw.BaseStream).ConfigAwait();
        }
Exemple #5
0
 private static PhotoEntity CreatePhoto(IHttpFile httpFile, PlaceEntity place)
 {
     return(new PhotoEntity
     {
         Title = httpFile.FileName,
         Image = httpFile.Data,
         ImageType = ImageMimeTypeConverter.ToImageType(httpFile.ContentType),
         Place = place
     });
 }
Exemple #6
0
    static object CreateFromHttpFileInfo(string filePath, IHttpFile file, Type intoType)
    {
        var obj = new Dictionary <string, object>
        {
            [Keywords.FilePath]          = filePath,
            [nameof(file.Name)]          = file.Name,
            [nameof(file.FileName)]      = file.FileName,
            [nameof(file.ContentLength)] = file.ContentLength,
            [nameof(file.ContentType)]   = file.ContentType ?? MimeTypes.GetMimeType(file.FileName),
        };
        var o = obj.FromObjectDictionary(intoType);

        return(o);
    }
        public Uri SaveImage(IHttpFile source, string folder)
        {
            folder.ThrowIfNullOrEmpty(nameof(folder));

            Uri result;
            var container = Client.GetContainerReference(Container);
            var filename  = GetRandomFilename(folder);

            container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Container, new BlobRequestOptions(),
                                             new OperationContext())
            .Wait();
            result = Upload(container, source.InputStream, filename).Uri;

            return(result);
        }
        public MediaFileTask(IHttpRequest httpHelper, IHttpFile httpFile, ILogWrite logWrite)
        {
            this._httpRequest = httpHelper;
            this._log         = logWrite;
            this._httpFile    = httpFile;

            FileInfo file = new FileInfo(LocalPath);

            if (!file.Directory.Exists)
            {
                file.Directory.Create();
            }
            downLoadThread = new Thread(DownLoadFileAsync);
            downLoadThread.Start();
        }
 public static void WriteTo(this IHttpFile httpFile, Stream stream)
 {
     httpFile.InputStream.WriteTo(stream);
 }
Exemple #10
0
 public static void SaveTo(this IHttpFile httpFile, IVirtualFiles vfs, string filePath)
 {
     vfs.WriteFile(filePath, httpFile.InputStream);
 }
        public static string UploadToImgur(this IHttpFile image, string imgurClientId, string paramName,
                                           int?minWidth = null, int?minHeight = null,
                                           int?maxWidth = null, int?maxHeight = null)
        {
            try
            {
                var imgurReq = WebRequest.Create("https://api.imgur.com/3/image");
                imgurReq.Headers[HttpHeaders.Authorization] = $"Client-ID {imgurClientId}";
                imgurReq.UploadFile(image.InputStream, image.FileName, MimeTypes.GetMimeType(image.FileName), field: "image");

                try
                {
                    using (var imgurRes = imgurReq.GetResponse())
                        using (var stream = imgurRes.GetResponseStream())
                        {
                            var resText = stream.ReadFully().FromUtf8Bytes();
                            var jsonRes = JSON.parse(resText);
                            if (jsonRes is Dictionary <string, object> jsonObj)
                            {
                                if (jsonObj["data"] is Dictionary <string, object> data)
                                {
                                    if (minWidth != null || maxWidth != null || minHeight != null || maxHeight != null)
                                    {
                                        var width  = (int)data["width"];
                                        var height = (int)data["height"];

                                        if (width < minWidth || height < minHeight)
                                        {
                                            throw new ArgumentException($"Minimum Dimensions {minWidth} x {minHeight}",
                                                                        paramName);
                                        }

                                        if (width > maxWidth || height > maxHeight)
                                        {
                                            throw new ArgumentException($"Maximum Dimensions {maxWidth} x {maxHeight}",
                                                                        paramName);
                                        }
                                    }

                                    if (data["link"] is string link && !string.IsNullOrEmpty(link))
                                    {
                                        return(link.Replace("\\/", "/"));
                                    }
                                }
                            }
                        }
                }
                catch (WebException e)
                {
                    var errorMessage = GetImgurErrorMessage(e.GetResponseBody());
                    if (errorMessage != null)
                    {
                        throw new ArgumentException(errorMessage);
                    }

                    throw;
                }

                throw new ArgumentException("Invalid Upload Image Response", paramName);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Could not upload image: " + ex.Message, paramName, ex);
            }
        }
 /// <summary>
 /// Add a new file.
 /// </summary>
 /// <param name="file">File to add.</param>
 public void Add(IHttpFile file)
 {
     if (file == null) throw new ArgumentNullException("file");
     _files.Add(file);
 }
Exemple #13
0
 public static async Task WriteToAsync(this IHttpFile httpFile, Stream stream)
 {
     await httpFile.InputStream.WriteToAsync(stream).ConfigAwait();
 }
Exemple #14
0
 public static async Task SaveToAsync(this IHttpFile httpFile, IVirtualFiles vfs, string filePath, CancellationToken token = default)
 {
     await vfs.WriteFileAsync(filePath, httpFile.InputStream, token).ConfigAwait();
 }
Exemple #15
0
 public static void SaveTo(this IHttpFile httpFile, string filePath)
 {
     using var sw = new StreamWriter(File.Create(filePath));
     httpFile.InputStream.WriteTo(sw.BaseStream);
 }
 /// <summary>
 /// Add a new file.
 /// </summary>
 /// <param name="file">File to add.</param>
 public void Add(IHttpFile file)
 {
     _files.Add(file.Name, file);
 }