Пример #1
0
        /// <summary>
        /// 将Free Problem Data转换为题目数据包
        /// </summary>
        /// <param name="fpd">Free Problem Data</param>
        /// <returns>题目数据包</returns>
        public static Byte[] ConvertFreeProblemDataToZipFile(List <FreeProblemDataPair> fpds)
        {
            if (fpds == null || fpds.Count < 1)
            {
                return(null);
            }

            ProblemDataWriter writer = new ProblemDataWriter();

            for (Int32 i = 0; i < fpds.Count; i++)
            {
                writer.WriteData(fpds[i].Input, fpds[i].Output);
            }

            return(writer.WriteTo());
        }
Пример #2
0
        /// <summary>
        /// 保存题目数据文件到磁盘
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <param name="forms">Request.Forms</param>
        /// <param name="httpFiles">Request.Files</param>
        /// <returns>是否保存成功</returns>
        public static IMethodResult AdminSaveProblemData(Int32 problemID, NameValueCollection forms, HttpFileCollectionBase httpFiles)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            SortedDictionary <String, String> dictData = new SortedDictionary <String, String>();

            if (httpFiles != null)
            {
                for (Int32 i = 0; i < httpFiles.Count; i++)
                {
                    HttpPostedFileBase file = httpFiles[i];

                    if (String.IsNullOrEmpty(file.FileName))
                    {
                        return(MethodResult.FailedAndLog("Filename can not be NULL!"));
                    }

                    FileInfo fi = new FileInfo(file.FileName);
                    if (httpFiles.GetKey(i).IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0 && !".in".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        return(MethodResult.FailedAndLog("Filename is INVALID!"));
                    }

                    if (httpFiles.GetKey(i).IndexOf("out", StringComparison.OrdinalIgnoreCase) == 0 && !".out".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase) && !".ans".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        return(MethodResult.FailedAndLog("Filename is INVALID!"));
                    }

                    if (file.ContentLength <= 0)
                    {
                        return(MethodResult.FailedAndLog("You can not upload empty file!"));
                    }

                    StreamReader sr = new StreamReader(file.InputStream);
                    dictData.Add(httpFiles.GetKey(i), sr.ReadToEnd());
                }
            }

            if (forms != null)
            {
                for (Int32 i = 0; i < forms.Count; i++)
                {
                    if (forms.GetKey(i).IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0 || forms.GetKey(i).IndexOf("out", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        dictData.Add(forms.GetKey(i), forms[i]);
                    }
                }
            }

            if (dictData.Count == 0)
            {
                return(MethodResult.FailedAndLog("No data was added!"));
            }

            if (dictData.Count % 2 != 0)
            {
                return(MethodResult.FailedAndLog("The count of uploaded data is INVALID!"));
            }

            ProblemDataWriter writer = new ProblemDataWriter();

            foreach (KeyValuePair <String, String> pair in dictData)
            {
                if (pair.Key.IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    String input  = pair.Value;
                    String output = String.Empty;

                    if (!dictData.TryGetValue(pair.Key.ToLower().Replace("in", "out"), out output))
                    {
                        return(MethodResult.FailedAndLog("The count of uploaded data is INVALID!"));
                    }

                    writer.WriteData(input, output);
                }
            }

            String fileNewName = problemID.ToString() + ".zip";
            String savePath    = Path.Combine(ConfigurationManager.ProblemDataPath, fileNewName);

            Byte[] data = writer.WriteTo();
            File.WriteAllBytes(savePath, data);

            ProblemDataCache.RemoveProblemDataVersionCache(problemID);

            return(MethodResult.SuccessAndLog("Admin create problem data, id = {0}", problemID.ToString()));
        }