public static string DoConvert(HttpContext context)
        {
            _fileName = context.Request["filename"];

            var extension         = (Path.GetExtension(_fileName) ?? "").Trim('.');
            var internalExtension = FileType.GetInternalExtension(_fileName).Trim('.');

            if (ConvertExts.Contains("." + extension) &&
                !string.IsNullOrEmpty(internalExtension))
            {
                var key = ServiceConverter.GenerateRevisionId(FileUri(_fileName));

                string newFileUri;
                var    result = ServiceConverter.GetConvertedUri(FileUri(_fileName), extension, internalExtension, key, true, out newFileUri);
                if (result != 100)
                {
                    return("{ \"step\" : \"" + result + "\", \"filename\" : \"" + _fileName + "\"}");
                }

                var fileName = GetCorrectName(Path.GetFileNameWithoutExtension(_fileName) + "." + internalExtension);

                var req = (HttpWebRequest)WebRequest.Create(newFileUri);

                using (var stream = req.GetResponse().GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("Stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(StoragePath + fileName, FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);
                        }
                    }
                }

                File.Delete(StoragePath + _fileName);
                _fileName = fileName;
            }

            return("{ \"filename\" : \"" + _fileName + "\"}");
        }
Beispiel #2
0
        private static void Get(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var fileName = context.Request["fileName"];

            if (string.IsNullOrEmpty(fileName))
            {
                context.Response.Write("error");
                return;
            }
            try
            {
                var fileUri     = EditDefault.FileUri(fileName);
                var key         = ServiceConverter.GenerateRevisionId(EditDefault.CurUserHostAddress + "/" + Path.GetFileName(fileUri));
                var validateKey = ServiceConverter.GenerateValidateKey(key);
                context.Response.Write(String.Format("{{ \"fileUri\": \"{0}\", \"key\": \"{1}\", \"validateKey\": \"{2}\" }}", fileUri, key, validateKey));
            }
            catch (Exception e)
            {
                context.Response.Write("{ \"error\": \"" + e.Message + "\"}");
            }
        }
 public static string GetExternalUri(string localUri)
 {
     try
     {
         var uri = HttpRuntime.Cache.Get(localUri) as string;
         if (string.IsNullOrEmpty(uri))
         {
             var webRequest = WebRequest.Create(localUri);
             using (var response = webRequest.GetResponse())
                 using (var responseStream = response.GetResponseStream())
                 {
                     var key = ServiceConverter.GenerateRevisionId(localUri);
                     uri = ServiceConverter.GetExternalUri(responseStream, response.ContentLength, response.ContentType, key);
                 }
             HttpRuntime.Cache.Insert(localUri, uri, null, DateTime.UtcNow.Add(TimeSpan.FromMinutes(2)), Cache.NoSlidingExpiration);
         }
         return(uri);
     }
     catch (Exception)
     {
     }
     return(localUri);
 }
Beispiel #4
0
        private static void Save(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            var downloadUri = context.Request["fileuri"];
            var fileName    = context.Request["filename"];

            if (string.IsNullOrEmpty(downloadUri) || string.IsNullOrEmpty(fileName))
            {
                context.Response.Write("error");
                return;
            }

            var newType     = Path.GetExtension(downloadUri).Trim('.');
            var currentType = (context.Request["filetype"] ?? Path.GetExtension(fileName)).Trim('.');

            if (newType.ToLower() != currentType.ToLower())
            {
                var key = ServiceConverter.GenerateRevisionId(downloadUri);

                string newFileUri;
                try
                {
                    var result = ServiceConverter.GetConvertedUri(downloadUri, newType, currentType, key, false, out newFileUri);
                    if (result != 100)
                    {
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    context.Response.Write("error");
                    return;
                }
                downloadUri = newFileUri;
                newType     = currentType;
            }

            fileName = Path.GetFileNameWithoutExtension(fileName) + "." + newType;

            var req = (HttpWebRequest)WebRequest.Create(downloadUri);

            try
            {
                using (var stream = req.GetResponse().GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("stream is null");
                    }
                    const int bufferSize = 4096;

                    using (var fs = File.Open(EditDefault.StoragePath + fileName, FileMode.Create))
                    {
                        var buffer = new byte[bufferSize];
                        int readed;
                        while ((readed = stream.Read(buffer, 0, bufferSize)) != 0)
                        {
                            fs.Write(buffer, 0, readed);
                        }
                    }
                }
            }
            catch (Exception)
            {
                context.Response.Write("error");
                return;
            }

            context.Response.Write("success");
        }