コード例 #1
0
 public HttpResponseMessage DownloadFile(FileDTO req)
 {
     try
     {
         var    folderManager = FolderManager.Instance;
         var    fileManager   = FileManager.Instance;
         string RawImageUrl   = req.url;
         if (RawImageUrl.IndexOf('?') > 0)
         {
             RawImageUrl = RawImageUrl.Substring(0, RawImageUrl.IndexOf('?'));
         }
         RawImageUrl = RawImageUrl.Replace(PortalSettings.HomeDirectory, "");
         var    file         = fileManager.GetFile(ActiveModule.PortalID, RawImageUrl);
         string uploadfolder = "OpenContent/Files/" + ActiveModule.ModuleID;
         if (!string.IsNullOrEmpty(req.uploadfolder))
         {
             uploadfolder = req.uploadfolder;
         }
         var userFolder = folderManager.GetFolder(PortalSettings.PortalId, uploadfolder);
         if (userFolder == null)
         {
             userFolder = folderManager.AddFolder(PortalSettings.PortalId, uploadfolder);
         }
         string fileName = FileUploadController.CleanUpFileName(Path.GetFileName(req.url));
         if (file == null && (req.url.StartsWith("http://") || req.url.StartsWith("https://")))
         {
             int    suffix       = 0;
             string baseFileName = Path.GetFileNameWithoutExtension(req.url);
             string extension    = Path.GetExtension(req.url);
             var    fileInfo     = fileManager.GetFile(userFolder, fileName);
             while (fileInfo != null)
             {
                 suffix++;
                 fileName = baseFileName + "-" + suffix + extension;
                 fileInfo = fileManager.GetFile(userFolder, fileName);
             }
             using (WebClient myWebClient = new WebClient())
             {
                 try
                 {
                     var stream = new MemoryStream(myWebClient.DownloadData(req.url));
                     file = fileManager.AddFile(userFolder, fileName, stream, true);
                 }
                 catch (Exception ex)
                 {
                     return(Request.CreateResponse(HttpStatusCode.OK, new
                     {
                         url = "",
                         id = -1,
                         error = ex.Message
                     }));
                 }
             }
         }
         return(Request.CreateResponse(HttpStatusCode.OK, new
         {
             url = file.ToUrl(),
             id = file.FileId,
             error = ""
         }));
     }
     catch (Exception exc)
     {
         Logger.Error(exc);
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc));
     }
 }
コード例 #2
0
        private void UploadWholeFile(HttpContextBase context, ICollection <FilesStatus> statuses)
        {
            IFileManager   _fileManager   = FileManager.Instance;
            IFolderManager _folderManager = FolderManager.Instance;

            for (var i = 0; i < context.Request.Files.Count; i++)
            {
                var file = context.Request.Files[i];
                if (file == null)
                {
                    continue;
                }

                var fileName = FileUploadController.CleanUpFileName(Path.GetFileName(file.FileName));


                if (IsAllowedExtension(fileName))
                {
                    string uploadfolder = "OpenContent/FormFiles/" + ActiveModule.ModuleID;

                    if (!string.IsNullOrEmpty(context.Request.Form["uploadfolder"]))
                    {
                        uploadfolder = context.Request.Form["uploadfolder"];
                    }
                    var userFolder = _folderManager.GetFolder(PortalSettings.PortalId, uploadfolder);
                    if (userFolder == null)
                    {
                        // Get folder mapping
                        var folderMapping = FolderMappingController.Instance.GetFolderMapping(PortalSettings.PortalId, "Secure");
                        userFolder = _folderManager.AddFolder(folderMapping, uploadfolder);
                        //userFolder = _folderManager.AddFolder(PortalSettings.PortalId, uploadfolder);
                    }
                    int    suffix       = 0;
                    string baseFileName = Path.GetFileNameWithoutExtension(fileName);
                    string extension    = Path.GetExtension(fileName);
                    var    fileInfo     = _fileManager.GetFile(userFolder, fileName);
                    while (fileInfo != null)
                    {
                        suffix++;
                        fileName = baseFileName + "-" + suffix + extension;
                        fileInfo = _fileManager.GetFile(userFolder, fileName);
                    }
                    fileInfo = _fileManager.AddFile(userFolder, fileName, file.InputStream, true);
                    var fileIcon = IconController.IconURL("Ext" + fileInfo.Extension, "32x32");
                    if (!File.Exists(context.Server.MapPath(fileIcon)))
                    {
                        fileIcon = IconController.IconURL("File", "32x32");
                    }

                    statuses.Add(new FilesStatus
                    {
                        success       = true,
                        name          = fileName,
                        extension     = fileInfo.Extension,
                        type          = fileInfo.ContentType,
                        size          = file.ContentLength,
                        progress      = "1.0",
                        url           = _fileManager.GetUrl(fileInfo),
                        thumbnail_url = fileIcon,
                        message       = "success",
                        id            = fileInfo.FileId,
                    });
                }
                else
                {
                    statuses.Add(new FilesStatus
                    {
                        success = false,
                        name    = fileName,
                        message = "File type not allowed."
                    });
                }
            }
        }