Example #1
0
        public ActionResult FileUpload(WebEditor.Models.FileUpload fileModel)
        {
            if (ModelState.IsValid)
            {
                if (!EditorDictionary.ContainsKey(fileModel.GameId))
                {
                    Logging.Log.ErrorFormat("FileUpload - game id {0} not in EditorDictionary", fileModel.GameId);
                    return(new HttpStatusCodeResult(500));
                }

                bool          continueSave = true;
                string        ext          = System.IO.Path.GetExtension(fileModel.File.FileName).ToLower();
                List <string> controlPermittedExtensions = EditorDictionary[fileModel.GameId].GetPermittedExtensions(fileModel.Key, fileModel.Attribute);
                if (fileModel.File != null &&
                    fileModel.File.ContentLength > 0 &&
                    s_serverPermittedExtensions.Contains(ext) &&
                    controlPermittedExtensions.Contains(ext))
                {
                    string filename = System.IO.Path.GetFileName(fileModel.File.FileName);
                    Logging.Log.DebugFormat("{0}: Upload file {1}", fileModel.GameId, filename);
                    string uploadPath = Services.FileManagerLoader.GetFileManager().UploadPath(fileModel.GameId);

                    // Check to see if file with same name exists
                    if (System.IO.File.Exists(System.IO.Path.Combine(uploadPath, filename)))
                    {
                        FileStream existingFile = new FileStream(System.IO.Path.Combine(uploadPath, filename), FileMode.Open);
                        // if files different, rename the new file by appending a Guid to the name
                        if (!FileCompare(fileModel.File.InputStream, existingFile))
                        {
                            // rename the file by adding a number [count] at the end of filename
                            filename = EditorUtility.GetUniqueFilename(fileModel.File.FileName);
                        }
                        else
                        {
                            continueSave = false; // skip saving if files are identical
                        }
                        existingFile.Close();
                    }

                    if (continueSave)
                    {
                        fileModel.File.SaveAs(System.IO.Path.Combine(uploadPath, filename));
                    }

                    ModelState.Remove("AllFiles");
                    fileModel.AllFiles = GetAllFilesList(fileModel.GameId);
                    ModelState.Remove("PostedFile");
                    fileModel.PostedFile = filename;
                }
                else
                {
                    ModelState.AddModelError("File", "Invalid file type");
                }
            }
            return(View(fileModel));
        }
Example #2
0
        public ActionResult FileUpload(FileUpload fileModel)
        {
            if (!ModelState.IsValid)
            {
                return View(fileModel);
            }

            if (!EditorDictionary.ContainsKey(fileModel.GameId))
            {
                Logging.Log.ErrorFormat("FileUpload - game id {0} not in EditorDictionary", fileModel.GameId);
                return new HttpStatusCodeResult(500);
            }

            var ext = Path.GetExtension(fileModel.File.FileName).ToLower();
            var controlPermittedExtensions = EditorDictionary[fileModel.GameId].GetPermittedExtensions(fileModel.Key, fileModel.Attribute);

            if (fileModel.File == null
                || fileModel.File.ContentLength == 0
                || !s_serverPermittedExtensions.Contains(ext)
                || !controlPermittedExtensions.Contains(ext))
            {
                ModelState.AddModelError("File", "Invalid file type");
                return View(fileModel);
            }

            var filename = Path.GetFileName(fileModel.File.FileName);
            Logging.Log.DebugFormat("{0}: Upload file {1}", fileModel.GameId, filename);
            var uploadPath = Services.FileManagerLoader.GetFileManager().UploadPath(fileModel.GameId);

            if (Config.AzureFiles)
            {
                var container = GetAzureBlobContainer("editorgames");
                var blob = container.GetBlockBlobReference(uploadPath + "/" + filename);

                var continueSave = true;

                if (blob.Exists())
                {
                    using (var ms = new MemoryStream())
                    {
                        blob.DownloadToStream(ms);
                        ms.Position = 0;
                        if (!FilesAreIdentical(fileModel.File.InputStream, ms))
                        {
                            filename = Path.GetFileNameWithoutExtension(fileModel.File.FileName) + " " + DateTime.UtcNow.ToString("yyyy-MM-dd HH.mm.ss") + Path.GetExtension(fileModel.File.FileName);
                            blob = container.GetBlockBlobReference(uploadPath + "/" + filename);
                        }
                        else
                        {
                            // skip saving if files are identical
                            continueSave = false;
                        }
                    }
                }

                if (continueSave)
                {
                    blob.Properties.ContentType = "application/octet-stream";
                    blob.UploadFromStream(fileModel.File.InputStream);
                }
            }
            else
            {
                var continueSave = true;

                // Check to see if file with same name exists
                if (System.IO.File.Exists(Path.Combine(uploadPath, filename)))
                {
                    FileStream existingFile = new FileStream(Path.Combine(uploadPath, filename), FileMode.Open);

                    if (!FilesAreIdentical(fileModel.File.InputStream, existingFile))
                    {
                        // rename the file by adding a number [count] at the end of filename
                        filename = EditorUtility.GetUniqueFilename(fileModel.File.FileName);
                    }
                    else
                    {
                        // skip saving if files are identical
                        continueSave = false;
                    }

                    existingFile.Close();
                }

                if (continueSave)
                {
                    var saveFile = Path.Combine(uploadPath, filename);
                    fileModel.File.SaveAs(saveFile);
                    UploadOutputToAzure(saveFile);
                }
            }

            ModelState.Remove("AllFiles");
            fileModel.AllFiles = GetAllFilesList(fileModel.GameId);
            ModelState.Remove("PostedFile");
            fileModel.PostedFile = filename;

            return View(fileModel);
        }