package com.l7tech.server;

import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.util.HexUtils;
import com.l7tech.util.IOUtils;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Timer;
import org.apache.commons.collections.map.LRUMap;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class FileDownloadServlet extends HttpServlet {
   private final int INITIAL_TASK_DELAY = 30000;
   private final int NORMAL_TASK_DELAY = 90000;
   private final int FILE_EXPIRY_PERIOD = 60000;
   private static final SecureRandom random;
   private final LRUMap fileMap = new LRUMap(10);
   private WebApplicationContext applicationContext;
   private Timer timer;
   static final long serialVersionUID = 2383879360811632234L;
   private static final String[] a;

   public void init(ServletConfig var1) throws ServletException {
      try {
         super.init(var1);
         this.applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
         if (this.applicationContext == null) {
            throw new ServletException(a[4]);
         }
      } catch (ServletException var2) {
         throw a(var2);
      }

      this.timer = (Timer)this.applicationContext.getBean(a[3], Timer.class);
      this.timer.schedule(new b3(this), 30000L, 90000L);
   }

   public void doPost(HttpServletRequest var1, HttpServletResponse var2) throws ServletException, IOException {
      String var3 = var1.getParameter(a[5]);
      byte[] var4 = IOUtils.slurpStream(var1.getInputStream(), 100000);
      byte[] var5 = new byte[20];
      random.nextBytes(var5);
      String var6 = HexUtils.hexDump(var5);
      synchronized (this.fileMap) {
         this.fileMap.put(var6, new z(var4, var3));
      }

      var2.setContentType(ContentTypeHeader.TEXT_DEFAULT.getFullValue());
      var2.getWriter().write(var6);
   }

   protected void doGet(HttpServletRequest var1, HttpServletResponse var2) throws ServletException, IOException {
      String[] var7 = a;
      String var3 = var1.getParameter(var7[6]);
      z var4;
      synchronized (this.fileMap) {
         var4 = (z)this.fileMap.get(var3);
      }

      try {
         if (var4 == null) {
            var2.sendError(404, a[0]);
            return;
         }
      } catch (ServletException var9) {
         throw a(var9);
      }

      var2.reset();
      var7 = a;
      var2.addHeader(var7[2], var7[1] + var4.b + "\"");
      var2.setContentType(var7[7]);
      ServletOutputStream var10 = var2.getOutputStream();
      var10.write(var4.a);
      var10.flush();
      var10.close();
   }

   private void removeExpiredFiles() {
      synchronized (this.fileMap) {
         if (!this.fileMap.isEmpty()) {
            for (Object var2 = this.fileMap.firstKey(); var2 != null; var2 = this.fileMap.nextKey(var2)) {
               z var3 = (z)this.fileMap.get(var2);
               long var4 = var3.a();
               long var6 = System.currentTimeMillis();
               if (var6 - var4 >= 60000L) {
                  this.fileMap.remove(var2);
               }

               if (this.fileMap.isEmpty()) {
                  return;
               }
            }
         }
      }
   }

   static {
      String[] var5 = new String[8];
      int var3 = 0;
      String var2 = "bm%<a\r\u0010D\"7u~\u001dSIz8of\u000b]\u0016Mv%}q\u0010\u001eIl%'2\u001e\u001a@g?}\u007f\u001dN\u000e\u0013om?hw\u0016\u0007\u0001F8ob\u0017\u0000Ev8s|\u0016Ac?}u\u001d\u0017nc2wu\n\u001cYl5H{\u0015\u0016^6om?z{\u001f\u0006^c%u}\u0016SIp#s`CSOm$pvX\u001dCvq{w\fSMr!p{\u001b\u0012Xk>r2\u001b\u001cBv4df\bJk=y|\u0019\u001eI";
      int var4 = "bm%<a\r\u0010D\"7u~\u001dSIz8of\u000b]\u0016Mv%}q\u0010\u001eIl%'2\u001e\u001a@g?}\u007f\u001dN\u000e\u0013om?hw\u0016\u0007\u0001F8ob\u0017\u0000Ev8s|\u0016Ac?}u\u001d\u0017nc2wu\n\u001cYl5H{\u0015\u0016^6om?z{\u001f\u0006^c%u}\u0016SIp#s`CSOm$pvX\u001dCvq{w\fSMr!p{\u001b\u0012Xk>r2\u001b\u001cBv4df\bJk=y|\u0019\u001eI"
         .length();
      char var1 = 21;
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 125;
         String var10001 = var2.substring(++var7, var7 + var1);
         byte var10002 = -1;

         while (true) {
            String var8 = a(var10000, a(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var8;
                  if ((var7 += var1) >= var4) {
                     a = var5;
                     random = new SecureRandom();
                     return;
                  }

                  var1 = var2.charAt(var7);
                  break;
               default:
                  var5[var3++] = var8;
                  if ((var7 += var1) < var4) {
                     var1 = var2.charAt(var7);
                     continue label24;
                  }

                  var2 = "tT\u001b\u0018~A\u0012CH(!kX\rA\u000e$#kT\u0016\u0002R?2zP\u000f";
                  var4 = "tT\u001b\u0018~A\u0012CH(!kX\rA\u000e$#kT\u0016\u0002R?2zP\u000f".length();
                  var1 = 3;
                  var7 = -1;
            }

            var10000 = 78;
            var10001 = var2.substring(++var7, var7 + var1);
            var10002 = 0;
         }
      }
   }

   private static ServletException a(ServletException var0) {
      return var0;
   }

   private static char[] a(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 14);
      }

      return var10000;
   }

   private static String a(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 81;
            case 1 -> 127;
            case 2 -> 44;
            case 3 -> 97;
            case 4 -> 111;
            case 5 -> 5;
            default -> 14;
         });
      }

      return new String(var10001).intern();
   }
}
