You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 line
6.5KB

  1. <%
  2. Class ImportController_Class
  3. Private m_useLayout
  4. Private m_title
  5. Private Sub Class_Initialize()
  6. m_useLayout = True
  7. m_title = "Import"
  8. End Sub
  9. Public Property Get useLayout
  10. useLayout = m_useLayout
  11. End Property
  12. Public Property Let useLayout(v)
  13. m_useLayout = v
  14. End Property
  15. Public Property Get Title
  16. Title = m_title
  17. End Property
  18. Public Property Let Title(v)
  19. m_title = v
  20. End Property
  21. '-------------------------------------------------------------------------------------------------------------------
  22. ' GET /import : upload form
  23. '-------------------------------------------------------------------------------------------------------------------
  24. Public Sub index()
  25. Dim csrf : csrf = Csrf_Token("import")
  26. %>
  27. <!--#include file="../views/Import/index.asp" -->
  28. <%
  29. End Sub
  30. '-------------------------------------------------------------------------------------------------------------------
  31. ' POST /import/upload : store the file, show the column-mapping step
  32. '-------------------------------------------------------------------------------------------------------------------
  33. Public Sub upload()
  34. Dim up, outName, outLocal, ext, token, saved, target, fso, tbl, csrf
  35. Dim headers, rows, sampleCount, fields
  36. Set fso = CreateObject("Scripting.FileSystemObject")
  37. Set up = New FreeASPUpload
  38. up.chunkSize = 1048576
  39. up.Upload
  40. If Not Csrf_Valid("import", up.Form("csrf")) Then
  41. If up.FormElements.Count = 0 Then
  42. Flash().AddError "Invalid form state: the upload was not received (" & Request.TotalBytes & " bytes). Please try again."
  43. ElseIf Len(up.Form("csrf")) = 0 Then
  44. Flash().AddError "Invalid form state: the form token was missing from the upload. Please try again."
  45. Else
  46. Flash().AddError "Invalid form state: your session expired or changed. Reload this page and try again."
  47. End If
  48. Response.Redirect "/import"
  49. End If
  50. If up.UploadedFiles.Count = 0 Then
  51. Flash().AddError "Choose a .csv, .xlsx or .xls file to import."
  52. Response.Redirect "/import"
  53. End If
  54. up.SaveOne ImportFile_UploadDir(), 0, outName, outLocal
  55. saved = ImportFile_UploadDir() & "\" & outLocal
  56. ext = LCase(Mid(outName, InStrRev(outName, ".") + 1))
  57. If ext <> "csv" And ext <> "xlsx" And ext <> "xls" Then
  58. If fso.FileExists(saved) Then fso.DeleteFile saved, True
  59. Flash().AddError "Only .csv, .xlsx and .xls files can be imported."
  60. Response.Redirect "/import"
  61. End If
  62. token = ImportFile_NewToken()
  63. target = ImportFile_UploadDir() & "\" & token & "." & ext
  64. fso.MoveFile saved, target
  65. Dim readError : readError = ""
  66. On Error Resume Next
  67. Set tbl = ImportFile_Read(target, 5)
  68. If Err.Number <> 0 Then
  69. readError = Err.Description
  70. Err.Clear
  71. End If
  72. On Error GoTo 0
  73. If Len(readError) = 0 Then
  74. If UBound(tbl("headers")) < 0 Then readError = "No columns were found in the first row."
  75. End If
  76. If Len(readError) > 0 Then
  77. If fso.FileExists(target) Then fso.DeleteFile target, True
  78. Flash().AddError "Could not read " & outName & ": " & readError
  79. Response.Redirect "/import"
  80. End If
  81. fields = ImportFile_Fields()
  82. headers = tbl("headers")
  83. Set rows = tbl("rows")
  84. csrf = Csrf_Token("import")
  85. Dim originalName : originalName = outName
  86. %>
  87. <!--#include file="../views/Import/map.asp" -->
  88. <%
  89. End Sub
  90. '-------------------------------------------------------------------------------------------------------------------
  91. ' POST /import/run : import every row using the chosen column mapping
  92. '-------------------------------------------------------------------------------------------------------------------
  93. Public Sub run()
  94. Dim token, path, fso, tbl, colOf, i, fieldKey, ncols, fields, res
  95. Dim defaultIndustry, markCustomers, originalName
  96. If Not Csrf_Valid("import", CStr(Request.Form("csrf"))) Then
  97. Flash().AddError "Invalid form state. Please try again."
  98. Response.Redirect "/import"
  99. End If
  100. token = CStr(Request.Form("token"))
  101. path = ImportFile_PathForToken(token)
  102. If Len(path) = 0 Then
  103. Flash().AddError "The uploaded file is no longer available. Please upload it again."
  104. Response.Redirect "/import"
  105. End If
  106. Set colOf = CreateObject("Scripting.Dictionary")
  107. ncols = 0
  108. If IsNumeric(CStr(Request.Form("ncols"))) Then ncols = CLng(CStr(Request.Form("ncols")))
  109. For i = 0 To ncols - 1
  110. fieldKey = CStr(Request.Form("map_" & i))
  111. If Len(fieldKey) > 0 Then
  112. If Not colOf.Exists(fieldKey) Then colOf.Add fieldKey, i
  113. End If
  114. Next
  115. ' Only accept known field keys
  116. Dim valid, f, known
  117. fields = ImportFile_Fields()
  118. For Each fieldKey In colOf.Keys
  119. known = False
  120. For Each f In fields
  121. If f(0) = fieldKey Then known = True
  122. Next
  123. If Not known Then colOf.Remove fieldKey
  124. Next
  125. If Not colOf.Exists("name") Then
  126. Flash().AddError "Map one column to ""Business name"" before importing."
  127. Response.Redirect "/import"
  128. End If
  129. defaultIndustry = Left(Trim(CStr(Request.Form("default_industry"))), 100)
  130. markCustomers = (CStr(Request.Form("mark_customers")) = "1")
  131. originalName = Left(CStr(Request.Form("original_name")), 255)
  132. Server.ScriptTimeout = 1800
  133. Set fso = CreateObject("Scripting.FileSystemObject")
  134. Set tbl = ImportFile_Read(path, 0)
  135. Set res = ImportService_Run(tbl, colOf, defaultIndustry, markCustomers, originalName)
  136. If Not res("failed") Then
  137. fso.DeleteFile path, True
  138. HTMLSecurity().ClearAntiCSRFToken "import"
  139. End If
  140. %>
  141. <!--#include file="../views/Import/result.asp" -->
  142. <%
  143. End Sub
  144. End Class
  145. Dim ImportController_Class__Singleton
  146. Function ImportController()
  147. If IsEmpty(ImportController_Class__Singleton) Then
  148. Set ImportController_Class__Singleton = New ImportController_Class
  149. End If
  150. Set ImportController = ImportController_Class__Singleton
  151. End Function
  152. %>

Powered by TurnKey Linux.