Exemplo n.º 1
0
        public static bool DeleteFile(FileD file)
        {
            bool deleted = false;

            if (file == null)
            {
                Logger.WriteLog("Input value is null, function DeleteFile()", LogLevel.Error);
                return(false);
            }
            if (File.Exists(CurrentDirectory + file.file_path + file.file_name))
            {
                File.Delete(CurrentDirectory + file.file_path + file.file_name);
                deleted = Database.file.DeleteById(file.file_id);
                if (deleted == false)
                {
                    Logger.WriteLog("Database does not contain file with id=" + file.file_id, LogLevel.Usual);
                }
                Logger.WriteLog("Delete file id=" + file.file_id, LogLevel.Usual);
                return(true);
            }
            else
            {
                Logger.WriteLog("Input file->" + file.file_path + file.file_name + " not exists, function DeleteFile", LogLevel.Error);
                return(false);
            }
        }
Exemplo n.º 2
0
        public App UploadAPK(FileD file, string hash)
        {
            string        pathAPK   = Full_Path_Upload + hash;
            DirectoryInfo directory = Directory.CreateDirectory(pathAPK);

            try
            {
                ZipFile.ExtractToDirectory(file.file_path + file.file_last_name, pathAPK);
            }
            catch (Exception)
            {
                Logger.WriteLog("Cannot unzip file, name=" + file.file_name, LogLevel.Error);
                return(null);
            }
            App apk = new App
            {
                app_id       = file.file_id,
                app_hash     = hash,
                archive_name = file.file_last_name,
                url_manifest = "http://" + Domen + "/YobiApp" + Path_Relative_Upload + hash + "/" + file.file_name,
                install_link = "http://" + Domen + "/YobiApp" + Path_Relative_Upload + hash + "/" + file.file_name,
                created_at   = (int)(DateTime.UtcNow - new DateTime(1970, 0, 0, 1, 1, 1)).TotalSeconds
            };

            Logger.WriteLog("Uploaded APK file. Hash=" + hash, LogLevel.Usual);
            return(apk);
        }
Exemplo n.º 3
0
        public void DeleteApp(ref string request, ref Socket remoteSocket)
        {
            string hash = Worker.FindParamFromRequest(ref request, "hash", TypeCode.String);

            if (hash == null)
            {
                Worker.JsonAnswer(false, "Can not find hash in this request params.", ref remoteSocket);
                return;
            }
            App app = Database.app.SelectByHash(hash);

            if (app == null)
            {
                Worker.JsonAnswer(false, "Can not find application by this hash.", ref remoteSocket);
                return;
            }
            FileD file = Database.file.SelectById(app.app_id);

            if (file != null)
            {
                LoaderFile.DeleteFile(file);
            }
            Worker.uploader.DeleteDirectory(app.app_hash);
            Worker.JsonAnswer(true, "Build completely deleted.", ref remoteSocket);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Multis the loading files. Get files from ascii request, create files in common folder and get all information about this files.
        /// </summary>
        /// <returns>The loading.</returns>
        /// <param name="AsciiRequest">ASCII request.</param>
        /// <param name="buffer">Buffer.</param>
        /// <param name="bytes">Bytes.</param>
        /// <param name="count_files">Count files.</param>
        public static FileD[] LoadingFiles(ref string AsciiRequest, ref byte[] buffer, ref int bytes, ref int count_files)
        {
            bool         endRequest        = false;
            int          last_position     = 0;
            List <Match> dispositionsAscii = new List <Match>();
            List <Match> boundariesAscii   = new List <Match>();

            FileD[] files = new FileD[count_files];
            if (CheckHeadersFileRequest(ref AsciiRequest))
            {
                string EndBoundaryAscii   = GetBoundary(ref AsciiRequest);
                Regex  endBoundaryPattern = new Regex(EndBoundaryAscii);
                while (!endRequest)
                {
                    Match contentFile = ContentDispositionPattern.Match(AsciiRequest, last_position);
                    if (contentFile.Success && boundariesAscii.Count < count_files)
                    {
                        last_position = contentFile.Index + contentFile.Length;
                        Match boundary = endBoundaryPattern.Match(AsciiRequest, last_position);
                        if (boundary.Success)
                        {
                            dispositionsAscii.Add(contentFile);
                            boundariesAscii.Add(boundary);
                        }
                    }
                    else
                    {
                        endRequest = true;
                    }
                }
                for (int i = 0; i < dispositionsAscii.Count; i++)
                {
                    Match  disposition = dispositionsAscii[i];
                    Match  boundaries  = boundariesAscii[i];
                    byte[] fileBuffer  = GetFileBufferByPositions(ref buffer, ref AsciiRequest, ref disposition, ref boundaries);
                    if (fileBuffer != null)
                    {
                        FileD file = new FileD();
                        if (CreateFileByInfo(ref disposition, ref file))
                        {
                            file.data = fileBuffer;
                            files[i]  = file;
                        }
                    }
                    else
                    {
                        Logger.WriteLog("Can not create file from request, file_count=" + i, LogLevel.Error);
                    }
                }
            }
            else
            {
                Logger.WriteLog("Request doesnot has required request fields.", LogLevel.Error);
                return(null);
            }
            Logger.WriteLog("Get files from request. From request loaded " + files.Length + " file(s).", LogLevel.Usual);
            return(files);
        }
 public bool GetFileRequest(ref FileD file)
 {
     if (LoaderFile.LoadingFile(ref data, ref buffer, ref bytes, ref file))
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 6
0
        public static bool CreateFileByInfo(ref Match disposition, ref FileD file)
        {
            if (disposition == null)
            {
                Logger.WriteLog("Input value is null, function CreateFileByInfo()", LogLevel.Error);
                return(false);
            }
            string ContentType = GetContentType(disposition.Value);

            file.file_extension = GetFileExtention(ContentType);
            file.file_type      = GetFileType(disposition.Value);
            file.file_last_name = GetFileName(disposition.Value);
            file.file_name      = random.Next(0, 2146567890).ToString();
            file.file_path      = GetPathFromExtention(file.file_extension);
            file.file_fullpath  = file.file_path + file.file_name;
            Logger.WriteLog("Create file info by disposition.", LogLevel.Usual);
            return(true);
        }
Exemplo n.º 7
0
        public App UploadIpa(FileD file, string hash)
        {
            string        pathIpa   = Full_Path_Upload + hash;
            DirectoryInfo directory = Directory.CreateDirectory(pathIpa);

            try
            {
                ZipFile.ExtractToDirectory(file.file_path + file.file_name, pathIpa);
            }
            catch (Exception)
            {
                Logger.WriteLog("Cannot unzip file, name=" + file.file_name, LogLevel.Usual);
                return(null);
            }
            string plistPath = SearchPathToFile("Info.plist", pathIpa);

            if (plistPath == "")
            {
                Logger.WriteLog("Cannot search plist file", LogLevel.Usual);
                return(null);
            }
            App ipa = GetIpaSet(plistPath, pathIpa);

            if (ipa == null)
            {
                return(null);
            }
            ipa.app_id       = file.file_id;
            ipa.app_hash     = hash;
            ipa.archive_name = file.file_name;
            ipa.url_manifest = "http://" + Domen + "/YobiApp" + Path_Relative_Upload + ipa.app_hash + "/" + ipa.archive_name;
            ipa.install_link = "itms-services://?action=download-manifest&url=https://" + Ssl_Domen + "/Plist/" + ipa.app_hash + ".plist";
            ipa.created_at   = (int)(DateTime.UtcNow - new DateTime(1970, 0, 0, 1, 1, 1)).TotalSeconds;
            if (!CreateInstallPlist(ipa))
            {
                return(null);
            }
            Logger.WriteLog("Uploaded Ipa file. Hash=" + hash, LogLevel.Usual);
            return(ipa);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Save file by specific path and get filled FileD structure data by that.
 /// <summary>
 public static bool SaveFile(ref FileD fileD, string RelativePath)
 {
     ChangeDailyPath();
     if (!Directory.Exists(CurrentDirectory + RelativePath + DailyPath))
     {
         Directory.CreateDirectory(CurrentDirectory + RelativePath + DailyPath);
     }
     if (!File.Exists(CurrentDirectory + RelativePath + DailyPath + fileD.file_name))
     {
         fileD.file_path     = RelativePath + DailyPath;
         fileD.file_fullpath = CurrentDirectory + RelativePath + DailyPath;
         CreateFileBinary(ref fileD.file_name, ref fileD.file_fullpath, ref fileD.data);
         Database.file.AddFile(fileD);
         Logger.WriteLog("Create file. file_name->" + fileD.file_name + " Relative path->" + RelativePath, LogLevel.Usual);
         return(true);
     }
     else
     {
         Logger.WriteLog("Can't create file. file_name->" + fileD.file_name + " Relative path->" + RelativePath, LogLevel.Warning);
         return(false);
     }
 }
Exemplo n.º 9
0
        public static bool LoadingFile(ref string AsciiRequest, ref byte[] buffer, ref int bytes, ref FileD file)
        {
            string log_text      = null;
            int    last_position = 0;

            if (CheckHeadersFileRequest(ref AsciiRequest))
            {
                string EndBoundaryAscii   = GetBoundary(ref AsciiRequest);
                Regex  endBoundaryPattern = new Regex(EndBoundaryAscii);
                Match  disposition        = ContentDispositionPattern.Match(AsciiRequest, last_position);
                if (disposition.Success)
                {
                    last_position = disposition.Index + disposition.Length;
                    Match boundary = endBoundaryPattern.Match(AsciiRequest, last_position);
                    if (boundary.Success)
                    {
                        byte[] fileBuffer = GetFileBufferByPositions(ref buffer, ref AsciiRequest, ref disposition, ref boundary);
                        if (fileBuffer != null)
                        {
                            if (CreateFileByInfo(ref disposition, ref file))
                            {
                                file.data = fileBuffer;
                                Logger.WriteLog("Get file from request. file.file_id->" + file.file_id, LogLevel.Usual);
                                return(true);
                            }
                            else
                            {
                                log_text = "Can not create file with buffer by positions.";
                            }
                        }
                        else
                        {
                            log_text = "Can not get file's buffer by positions.";
                        }
                    }
                    else
                    {
                        log_text = "Can not define boundary of request.";
                    }
                }
                else
                {
                    log_text = "Can not define content-disposition of request.";
                }
            }
            else
            {
                log_text = "Request doesn't has required request's fields.";
            }
            Logger.WriteLog(log_text, LogLevel.Warning);
            return(false);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Saves the file by ref file_name, file_path and data. Set this paramaters if you want to change full_path, file_name and data of file.
 /// </summary>
 /// <param name="file">File.</param>
 public void SaveFile(ref FileD file)
 {
     LoaderFile.CreateFileBinary(ref file.file_name, ref file.file_path, ref file.data);
     Database.file.AddFile(file);
 }
Exemplo n.º 11
0
        public void UploadBuild(ref string request, ref byte[] buffer, ref int bytes, ref Socket remoteSocket)
        {
            List <FileD> files = LoaderFile.LoadingFiles(ref request, ref buffer, ref bytes, ref count);

            if (files != null)
            {
                if (files.Count == 1)
                {
                    FileD  file     = files[0];
                    string app_hash = Worker.uploader.GenerateHash(8);
                    if (file.file_type == "application")
                    {
                        Directory.CreateDirectory(Worker.uploader.Full_Path_Upload + app_hash);
                        App app = null;
                        switch (file.file_extension)
                        {
                        case "vnd.android.package-archive":
                            app = Worker.uploader.UploadAPK(file, app_hash);
                            break;

                        case "octet-stream":
                            app = Worker.uploader.UploadIpa(file, app_hash);
                            break;

                        default:
                            LoaderFile.DeleteFile(file);
                            Worker.uploader.DeleteDirectory(app_hash);
                            Worker.JsonAnswer(false, "Archive type that uploaded is wrong.", ref remoteSocket);
                            break;
                        }
                        if (app == null)
                        {
                            LoaderFile.DeleteFile(file);
                            Worker.uploader.DeleteDirectory(app_hash);
                            Worker.JsonAnswer(false, "Error of handle upload archive.", ref remoteSocket);
                            return;
                        }
                        int?uid = Worker.FindParamFromRequest(ref request, "uid", TypeCode.Int32);
                        if (uid != null)
                        {
                            if (Database.user.SelectId(uid) != null)
                            {
                                app.user_id = (int)uid;
                            }
                        }
                        else
                        {
                            app.user_id = -1;
                        }
                        Worker.JsonData(app, ref remoteSocket);
                        if (file.file_extension == "vnd.android.package-archive")
                        {
                            app = Worker.uploader.GetAPKSet(Worker.uploader.Full_Path_Upload + app_hash, app);
                        }
                        Database.app.Add(app);
                    }
                }
                else
                {
                    foreach (FileD file in files)
                    {
                        LoaderFile.DeleteFile(file);
                    }
                    Worker.JsonAnswer(false, "Get from request not required count files.", ref remoteSocket);
                }
            }
            else
            {
                Worker.JsonAnswer(false, "Can't get file from request.", ref remoteSocket);
            }
        }